Get values from mysql table into dropdown menu (undefined variable) [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.
this is probably a very simple question to solve, however I've been stuck with this for a while and I can't figure out for the life of me what's wrong with my code. It may be just a syntax mistake but I've gathered this code from other questions and it should work but I keep getting the errors : - Undefined variable : mysqli and - Call to a member function query() of a non-object , both in the line "$result = $mysqli->query($sql);".
Here's the snippet of my code where I have the dropdown menu set up.
<label class="control-label" for="formInput85">Professor</label>
<?php
$sql = "SELECT name FROM professores";
$result = $mysqli->query($sql);
echo "<select class=".'"form-control"'.">";
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";
?>
And here's my code in the very beginning of the page, that is connecting to my database in phpmyadmin.
<?php
session_start();
echo $_SESSION['name'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "teste";
$conn = new mysqli($servername,$username,$password,$dbname);
?>
Thank you for your help! If you have any tips on how to send the value selected into another row of the table I gladly appreciate it as it will be my next step :)

$mysqli is not the right variable in this case. So this line:
$result = $mysqli->query($sql);
has to become
$result = $conn->query($sql);

Related

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.

Undefined offset trying to echo data from database [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 am trying to echo out the number of agility that is set in the database. This is my current code, if you could check it out and see if there is anything i need to change to get it working. Currently im getting the error
Notice: Undefined offset: 0 in
$dbserver = "localhost";
$dbusername = "root";
$dbpassword = "*******";
$db = "********";
//CREATE CONNECTION
$conn = new mysqli($dbserver, $dbusername, $dbpassword, $db);
$id = $_SESSION['loggedin'];
$query = "SELECT * FROM stats WHERE id='$id'";
$stmt = mysqli_query($conn, $query);
$result = mysqli_fetch_all($stmt,MYSQLI_ASSOC);
<div class="Agility">
<h2>Agility</h2>
<p><?php echo $result[0]['agility']; ?></p>
</div>
First off, since your id field is probably unique, you should limit the query to one. Then, check the number of rows returned by the query before displaying the results in case there are none.
$query = "SELECT * FROM stats WHERE id='$id' LIMIT 1";
$stmt = mysqli_query($conn, $query);
if(mysqli_num_rows($stmt) > 0){
$result = mysqli_fetch_all($stmt,MYSQLI_ASSOC);
?>
<div class="Agility">
<h2>Agility</h2>
<p><?php echo $result[0]['agility']; ?></p>
</div>
<?php } else { ?>
<p>There are no results.</p>
<?php } ?>

What did I do wrong in my Database Query with 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.
I have a database and a table in it which is looking like this
ID Picture Description.
In my PHP-Code I try to get the "Picture" which is just a text right now and the Description. But I always get
Undefined Index: Description
Undefined Index: Picture
Here my Code:
<?php include ("db.php");
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Picture, Description FROM family WHERE ID = 1 ";
$result = $conn->query($sql)
or die ("MySQL-Error: " . $conn->error);
if ($result->num_rows >0) {
while ($row = mysqli_fetch_row($result)){
echo "Pic: " . $row["Picture"]. " - Description: " . $row["Description"]. "
}
}
else {
echo "Not good";
}
$conn->close();
echo "Connected successfully"; ?>
What does the error mean
EDIT: I solved it changed mysqli_fetch_row to mysqli_fetch_assoc
mysqli_fetch_row returns an enumerated array starting at 0, not an associative array. See: http://php.net/manual/en/mysqli-result.fetch-row.php
Here's the issue:
while ($row = mysqli_fetch_row($result))
you should use mysqli_fetch_array() or mysqli_fetch_assoc()

Call PHP function from html form with parameters [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 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);
}

Categories