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 have simple inventory DB. When trying to access the products table and display the data in a table, i continually get "Unidentified index" and nothing i have found thus far can assist me. I am just starting PHP
conn.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "slfs_storesb";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_errno) {
printf("Connection failed: %s\n" . $conn->connect_error);
}
?>
index.php
<?php
$sql = "select * from tbl_station";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)) {
var_dump($row);
// print $row[4];
//echo '<table id="t01">';
echo $row["Station_id"]." ".$row["Station_name"]." ". $row["Station_email"];
// echo '</table>';
}
// } else {
// echo "0 results";
// }
?>
THis is the error
Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
Gros Islet Fire Station
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
GFL Fire Hall
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
HeadQuarters
( ! ) Notice: Undefined index: Station_id in C:\wamp\www\stores\index_1.php on line 21
Call Stack
# Time Memory Function Location
1 0.0010 246504 {main}( ) ..\index_1.php:0
Dennery class footer { This is footer }
Instead of using SELECT * just spell out the (three) fields you want to access.
This way the query will fail if one of the fields is misspelled (or simply doesn't exist in that table).
<?php
$sql = '
SELECT
Station_id, Station_name, Station_email
FROM
tbl_station
';
$result = $conn->query($sql)
or trigger_error('query failed: '.join(',', $conn->error_list));
while( $row=$result->fetch_assoc() ) {
echo $row['Station_id'], ' ', $row['Station_name'],' ', $row["Station_email"], "<br />\r\n";
}
Related
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.
getting error
Notice: Undefined index: device_id in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 17
Notice: Undefined index: date_and_time in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 18
Notice: Undefined index: latitude in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 19
Notice: Undefined index: longitude in C:\xampp\htdocs\Real-Time-master\server\gps-2-map\api\index.php on line 20
// Establish a connection to database
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'gps2db');
$db_connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
// Extract POST-data to variables
$device_id = mysqli_real_escape_string($db_connection, $_POST['device_id']);
$date_and_time = mysqli_real_escape_string($db_connection, $_POST['date_and_time']);
$latitude = mysqli_real_escape_string($db_connection, $_POST['latitude']);
$longitude = mysqli_real_escape_string($db_connection, $_POST['longitude']);
// Validate URL - If required parameters are found, continue processing
if(isset($device_id) && isset($date_and_time) && isset($latitude) && isset($longitude))
{
// Check that device corresponding to device_id aka MAC-address is found from database
$check_device = mysqli_query($db_connection,"SELECT device_id FROM devices WHERE device_id = '$device_id'");
// If device corresponding to device_id is found, continue processing
if(mysqli_num_rows($check_device))
{
// Find out if there is any existing location information in database for given device
Error happen because you process null data before you do validation. Try change variable declaration to this
$device_id = isset($_POST['device_id']) ? mysqli_real_escape_string($db_connection, $_POST['device_id']): null;
$date_and_time = isset($_POST['date_and_time']) ? mysqli_real_escape_string($db_connection, $_POST['date_and_time']): null;
$latitude = isset($_POST['latitude']) ? mysqli_real_escape_string($db_connection, $_POST['latitude']): null;
$longitude = isset($_POST['longitude']) ? mysqli_real_escape_string($db_connection, $_POST['longitude']): null;
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()
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.
The purpose of my lab is to have the user search through a database called "pageVistis" from what they enter in the field. They can search through the remote host or page name. I have Been having trouble fixing an error that i am recieving saying "Notice: Undefined variable: searchtype in C:\xampp\htdocs\pageVisitsLab\DataBC.php on line 23
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\pageVisitsLab\DataBC.php:25 Stack trace: #0 {main} thrown in C:\xampp\htdocs\pageVisitsLab\DataBC.php on line 25". Also the code is form a file called "DataBC.php". If any extra information is needed please let me know and i will edit this post and include it thank you.
include "interface.php";
if(isset($_POST['submit'])) {
$sName = $_POST['sN'];
$sHost = trim($_POST['sH']);
if(!$sName || ! $sHost) {
echo "<p> Please enter a search </p>";
exit;
}
/*switch ($sName) {
case 'sName':
case 'sHost':
break;
default:
echo "<p> Invalid search type and please try again </p>";
}
*/
$query = "SELECT page_name, visit_date, previous_page, request_method, remote_host, remote_port FROM visitInfo
WHERE $searchtype= ?";
$stmt = $databse -> prepare($query);
$stmt-> bind_param('s',$sName);
$stmt-> execute();
$stmt->store_result();
$stmt->bind_result($pageName,$visitDate,$previouPage,$requestMethod,$remoteHost,$remotePort);
echo "<p> The number of items found: .$stmt->num_rows</p>";
while ($stmt->fetch()) {
echo "<p><strong>page Name: ".$pageName."</strong></p>";
echo "Visit Date: ".$visitDate."<br>";
echo "Previous Page: ".$previouPage. "<br>";
echo " Request Method: ".$requestMethod. "<br>";
echo "Remote Host: ".$remoteHost. "<br>";
echo "Remote Port: ".$remotePort. "<br>";
}
$stmt->free_result();
$databse->close();
}
?>
I think your problem is in your query, is $searchtype a variable you define in php? maybe in that interface.php.
If you want to use a php variable in a mysql query then that is not the correct way of doing 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 7 years ago.
Here is the php connectivity code:
<?php
$con=mysql_connect("127.0.0.1", "root", "");
mysql_select_db("society");
if(!$con)
{
echo "Failed to Connect to MySql".mysqli_connect_error();
}
else
{
echo"success";
}
$query="INSERT INTO users (fname, lname, email, password)
VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')";
mysql_query($query,$con);
?>
This is the error. Tried, but I am not able to solve the error:
Notice: Undefined index: fname in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13
Notice: Undefined index: lname in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13
Notice: Undefined index: email in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13
Notice: Undefined index: password in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13
Try this
<?php
$con=mysql_connect("127.0.0.1", "root", "");
mysql_select_db("society");
if(!$con)
{
echo "Failed to Connect to MySql".mysql_connect_error();
}
else
{
echo"success";
}
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email'])) {
$query="INSERT INTO users (fname, lname, email, password)
VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')";
mysql_query($query,$con);
}
?>
Check if there is values for the POST your using in the query. If it is returning undefined_index is because the variables of the post that you are using isn't set. They don't even exist at the moment.
For instance:
If you debug $_POST['fname'] it will return false or empty. Try it.
Resolution:
Right now I think you're running this code without validating if the form has been submited or not.
So, to start you should do this:
if(isset($_POST) && count($_POST) > 0){
$con=mysql_connect("127.0.0.1", "root", "");
mysql_select_db("society");
if(!$con)
{
echo "Failed to Connect to MySql".mysqli_connect_error();
}
else
{
echo"success";
}
$query="INSERT INTO users (fname, lname, email, password) VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')";
mysql_query($query,$con);
}
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.
Am using PDO method to get data from SQL But am unable to get data from SQL database since am new to PDO its little hard to understand
but i did the following code but Doesn't work can someone help me
CODE
<?php
$servername = "localhost";
$username = "sanoj";
$password = "123456";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM members");
$stmt->execute();
// set the resulting array to associative
$membid=($_GET['memberID']);
$email=($_GET['email']);
$state=($_GET['username']);
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
echo $membid;
echo $email;
echo $state;
$conn = null;
echo "</table>";
?>
ERROR I GET
Notice: Undefined index: memberID in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 14
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
( ! ) Notice: Undefined index: email in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 15
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
( ! ) Notice: Undefined index: username in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 16
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
Using $_GET variable won't make you get your data from your database using PDO. You were doing right up until $stmt->execute();. What you need to do next is:
while($result = $stmt->fetch(PDO::FETCH_ASSOC))
{
foreach($result as $key => $value)
echo $key.': '.$value.'<br/>';
echo '<hr/>';
}
To get your data.
$_GET array is to fetch your data from the URL or from a GET Ajax call.