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()
Related
This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 1 year ago.
I'm having trouble putting values on my php and sending them to My sql.
<?php
$tempSerialNumber = $_GET['sn'];
$tempLatitude = $_GET['l1'];
$tempLongitude = $_GET['l2'];
$servername = "localhost";
$username = "bornitex_uptoyou";
$password = "12345678";
$dbname = "bornitex_TMS";
$conn = new mysqli($servername, $username,$password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$val1 = $_GET['tempSerialNumber'];
$val2 = $_GET['tempLatitude'];
$val3 = $_GET['tempLongitude'];
$sql = "INSERT INTO LogGPS (SerialNumber, Latitude, Longitude)
VALUES ($val1,$val2,$val3)";
if ($conn->query($sql) === TRUE) {
echo "save OK";
} else {
echo "Error:" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
The value I sent
http://uptoyoutest.bornitexpert.com/add02.php?sn=abc&l1=1.0&l2=2.0
error I got
Error:INSERT INTO LogGPS (SerialNumber, Latitude, Longitude) VALUES (,,)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',)' at line 2
The database I created
enter image description here
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.
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 } ?>
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.
UPD: CLOSED. Duplicate found and typos in my original question
I am using this code and I want to get a message if there is an error with my SQL query :
$Db = mysqli_init();
$Db->options(MYSQLI_OPT_LOCAL_INFILE, true);
$Db->real_connect($servername, $username, $password, $dbname, 3306);
// Creation of first SQL query
$sql = ('select sum('.$metric1.') as t1metric from '.$table1.' WHERE '.$date1.' between "'.$start_date.'" AND "'.$end_date.'"');
$query = $Db->query($sql);
if ($Db->error)
{
printf("Errormessage: %s\n", $Db->error);
}
and I receive this error when I run the php file :
Call to a member function query() on a non-object
use the following
$Db->query($sql);
instead of
$mysqli->query($query);
$mysqli->query($query);
Replace with:
$Db->query($query);
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);