Notice: Undefined index: id [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 have used a prepared statement to define the id index however, it is telling me that it is undefined for some reason, where and what do i change for this to work?
<?php
$db_username='student';
$db_password='student';
$db = new PDO ('mysql:host=192.168.56.2;dbname=Assessment', $db_username, $db_password);
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname='".$_GET['id']."' ");
$result->execute();

Try this:
<?php
$db_username='student';
$db_password='student';
$db = new PDO ('mysql:host=192.168.56.2;dbname=Assessment', $db_username, $db_password);
if(isset($_GET['id'])) {
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname=?");
$result->execute(array($_GET['id']);`enter code here
}
else {echo('$_GET["id"] not set');}

First, verify that $_GET['id'] has a value. Second, for security, change some lines:
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname=:id");
$result->execute(array(':id' => $_GET['id']));

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);

php echo json command [duplicate]

This question already has answers here:
What does the PHP error message "Notice: Use of undefined constant" mean?
(2 answers)
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
when i try to Sign up echo json commands for existing email i get this error
Use of undefined constant Email_Error - assumed 'Email_Error' (this will throw an Error in a future version of PHP) in C:\xamppnew\htdocs\android\signup.php on line 16
{"SIGNUP":"Email_Error"}
and when there is no error i get this error
Notice: Undefined variable: arr in C:\xamppnew\htdocs\android\signup.php on line 23
null
php code
<?php
include "conn.php";
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
$sql_verify = "SELECT * FROM users WHERE Email = :EMAIL";
$stmt = $PDO->prepare($sql_verify);
$stmt->bindParam(':EMAIL', $Email);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$arr = array( 'SIGNUP' => Email_Error);
}else{
echo "Signup compelete";
}
echo json_encode($arr);
?>
note im following php manual for json

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

Return SQL query errors in PHP file [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.
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);

PHP $_POST Notice: Undefined index: regno on line 33 [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've got this error. I thought it's on misplaced of ' the $_POST, but if I do that (eg $_POST['regno']) it will give T_ESCAPED error.
How I can fix this?
Here is the code of line 33:
$sql = "SELECT * FROM students WHERE RegNo='$_POST[regno]' AND
password='$_POST[password]' AND Status='Enabled'";
I'm using Wamp as my localhost.
Thanks!
You need to check the post value like this
$reg_no = isset($_POST['regno']) ? $_POST['regno'] : '';
$sql = "SELECT * FROM students WHERE RegNo='".$reg_no."' AND password='".$_POST['password']."' AND Status='Enabled'";
First be sure that your variables exists before trying to save them :
<?php
if(isset($_POST['regno']) && !empty($_POST['regno'])){
$regno =$_POST['regno'];
}
if(isset($_POST['password']) && !empty($_POST['password'])){
$password =$_POST['password'];
}
if(isset($password) && isset($regno)){
$sql = "SELECT * FROM students WHERE RegNo='$regno' AND password='$password' AND Status='Enabled'";
}
?>
Also keep in mind that using POST var like this is very exposed to SQL injections.
try this :
$sql = "SELECT * FROM students WHERE RegNo='".$_POST['regno']."' AND password='".$_POST['password']."' AND Status='Enabled'";

Categories