Call PHP function from html form with parameters [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I am trying to run a simple select on the DB from a HTML form
HTML snippet is
<form Name ="form1" Method ="post" ACTION = "get341Usage.php">
<input type="submit" name="Submit1" value="3 Months" >
</form>
PHP file get341Usage.php
<?php
function get341Usage($org_id,$usage_mnth ) {
$conn = oci_connect('user', 'pass', '//server/ora_instance');
$query = "SELECT usage.* from usage_table usage
where customer_number = ' . $cust_id . '
AND usage_date >= (select add_months(sysdate ,' . $usage_mnth . '))";
$stid = oci_parse($conn, $qryStr);
oci_execute($stid);
oci_free_statement($stid);
}
if(isset($_POST['submit']))
{
getUsage($org_id,$usage_mnth);
}
?>
the reason for the 2 paramters was I wanted to create 3 buttons 3,6,12 monnths where the user to clicks and it prompts to auto save teh data to a csv file (haven't even got to this pary yet!)
any points would be great ...I suspect I'm miles off

if(isset($_POST['Submit1']))
{
getUsage($org_id,$usage_mnth);
}

Related

How can I fix an undefined index? [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
I am connected to my local host and am trying to use a GET method on line $. I am getting the Notice: Undefined index: deleteid in C:\xampp\htdocs\webd153\delete.php on line 4.
<?php
include 'connection.php';
$deleteid = $_GET['deleteid'];
if (isset($deleteid)) {
$deletesql = $dbh->prepare("DELETE FROM users WHERE id = '$deleteid'");
$deletesql->execure();
echo "record has been deleted!<br>";
I am trying to delete names that I have entered in my databases using a form that is connected from my local host to myphpadmin database.
Right way is:
<?php
include 'connection.php';
if(isset($_GET['deleteid']) {
$deleteid = $_GET['deleteid'];
$deletesql = $dbh->prepare("DELETE FROM users WHERE id = '$deleteid'");
$deletesql->execute();
echo "record has been deleted!<br>";
}
But this is VERY INSECURE! When I send request with URL ending ?deleteid=1'+OR+1=1+OR+id=', your database will be deleted all rows. I suggest to change query building as:
$deletesql = $dbh->prepare("DELETE FROM users WHERE id = (?)");
$deletesql->bind_param('i', $deleteid);

how to fix undefined variable issue in code while extracting data from database in input field [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
I am trying to fetch the data from database for a an input field product code and i need to use its value to update the rest of the column values in the database but instead it is creating a different record and in the value field of the input box 'named' code, it shows and undefined variable error, please help.
HTML code:
<div class="small-8 columns">
<input type="text" id="right-label" placeholder="Product_code"
value="<?php echo "$pcode"?>" name="code">
</div>
PHP Script:
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="bolt";
try{
$conn = new
PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST["submit"])){
$pcode = ($_POST["code"]);
$pname = ($_POST["Pname"]);
$pdesc = ($_POST["desc"]);
$pimg = $_FILES["Img_name"]["temp_name"];
$imgExt = strtolower(pathinfo($pimg,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg','jpg','png','gif','pdf');
$pqty = ($_POST["Pqty"]);
$pprice = ($_POST["Pprice"]);
$sql="UPDATE products SET product_name=$pname,product_desc=$pdesc,
product_img_name=$pimg,qty=$pqty,price
=$pprice) WHERE product_code=$pcode";
$stmt = $conn->exec($sql);
$stmt->execute();
echo $stmt->rowCount() . "new records added succesfully";
}
}
catch(PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
$sql is declared within the if condition, if if(isset($_POST["submit"])){
is false, you will get this error because $sql is not within the scope. Declare it on above condition and initialize it.

Notice: Undefined index: namaunit [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I'm new in programming, and i don't know why it doesn't work. I use the same code for my other input form, and that code work just fine. But, for this form, it's not. The form is pretty much same, so, that's why i totally do not understand why it's not working.
This is the php code where the message error come from. I wish you can help me. Thank you so much.
<?php
session_start();
require 'db.php';
$kodeunit = $_SESSION["KodeUnit"];
$namaunit = $_POST['namaunit'];
$alamat = $_POST['alamat'];
$pimpinanunit = $_POST['pimpinanunit'];
$kuasaanggaran = $_POST['kuasaanggaran'];
$pembuatkomitmen = $_POST['pembuatkomitmen'];
$penanggungjawab = $_POST['penanggungjawab'];
$sql = "UPDATE unit_organisasi SET Nama_Unit = '$namaunit', Pimpinan_Unit = '$pimpinanunit', Alamat = '$alamat', Kuasa_Anggaran = '$kuasaanggaran', Pembuat_Komitment = '$pembuatkomitmen', Penanggungjawab = '$penanggungjawab' WHERE Kode_Unit = '$kodeunit'";
if((!strlen(trim($namaunit))) || (!strlen(trim($alamat))) || (!strlen(trim($pimpinanunit))) || (!strlen(trim($kuasaanggaran))) || (!strlen(trim($pembuatkomitmen))) || (!strlen(trim($penanggungjawab)))){
echo "<script>alert('Data Belum Lengkap!')</script>";
header ('Location:inpudataunit.php');
}
else{
$result = $conn->query($sql);
if($result === TRUE){
echo "Berhasil diinput";
}
}
?>
Seems like your form has no input field named namaunit . Check your form input name.

Trying to make a simple Sql query in PHP [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
Hello can you help me with this script?
Im trying to pull information on my slider from database.
$query = "SELECT * FROM `slider1`";
$select_from_slider1 = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_from_slider1)){
$slider1_title = $row['slider1_title'];
$slider1_content = $row['slider1_content'];
$slider1_moreinfo = $row['slider1_moreinfo'];
?>
<h2><?php echo $slider1_title ?></h2>
<p><?php echo $slider1_content ?></p>
<?php echo $slider1_moreinfo ?>
<?php } ?>
This is the error i get:
Lol im sorry i figured it out myself, the reason for the error was that i dublicated many times include "database"....
Thanks guys for the fast replies.!
Looks like the var $connection isn't a mysqli connection. You'll need the following somewhere this script can access it.
$connection = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
http://php.net/manual/en/mysqli.construct.php

username doesn't show when we login [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
login code
<?php
include("koneksi.php");
$email=$_POST['email'];
$password=md5($_POST['password']);
$q="SELECT * FROM `user` WHERE `user`.`email`='$email' AND `user`.`password`='$password'";
$qe=mysql_query($q);
while ($de=mysql_fetch_array($qe)) {
$id_user=$de['id_user'];
}
if (mysql_num_rows($qe)>0) {
session_start();
$_SESSION['x']=$id_users;
header('location:home.php');
exit;
} else{
header('location:login_user.php');
exit;
}
?>
after login i wanna show or echo the username with this code
<?php
session_start();
$id_user=$_SESSION['x'];
$q="SELECT * FROM `user` WHERE `user`.`id_user`='$id_user'";
$qe=mysql_query($q);
$de=mysql_fetch_array($qe);
$username=$de['username'];
echo "
<li>$username</li>
";
?>
and the problem is the username doesn't show..
whats wrong.. help me ..
You have a typo in your login search query. Change to $_SESSION['x'] = $id_user;
Not $id_users
A piece of advice, use an IDE such as netbeans or eclipse or phpstorm. They help in identifying unused variables and other minor syntax errors.

Categories