How can I fix an undefined index? [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)
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);

Related

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

Notice: Undefined index: action in /opt/lampp/htdocs/contacts.php on line 6 [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 4 years ago.
I was developing one simple PHP script in window machine and was testing in xampp. I have completed it and its working fine in my windows machine. Now I have tried to move it in my centos 7 machine which also have xampp. Its giving me error like below
Notice: Undefined index: action in /opt/lampp/htdocs/contacts.php on line 6
My code for that function is like below
if($_GET['action']=="change_status" && $_GET['contact_id']>0 )
{
$upd_qry = "update contacts set status = ? where id=?";
$stmt = mysqli_prepare($mysqli, $upd_qry);
mysqli_stmt_bind_param($stmt, "ii", $_GET['status'],$_GET['contact_id']);
$result = mysqli_stmt_execute($stmt);
$_SESSION['msg']="11";
header( "Location:contacts.php");
exit;
}
I have marked that similar errors in 1-2 more files too. I do not understanding why this happening. I have php 7.2 in my windows machine and in centos its 7.0
let me know if someone know what is wrong with it.
Thanks
The problem is that $_GET['action'] does not exist, when you don't add it in the URL. You can work it around by adding a isset-check
if(isset($_GET['action']) && $_GET['action']=="change_status" && $_GET['contact_id']>0 )
{
$upd_qry = "update contacts set status = ? where id=?";
$stmt = mysqli_prepare($mysqli, $upd_qry);
mysqli_stmt_bind_param($stmt, "ii", $_GET['status'],$_GET['contact_id']);
$result = mysqli_stmt_execute($stmt);
$_SESSION['msg']="11";
header( "Location:contacts.php");
exit;
}

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.

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'";

Notice: Undefined index: $username [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 8 years ago.
Hello i'm new to php and i wanted to create a admin panel and i got an error with my register page.
This is the error :
Notice: Undefined index: username in C:\wamp\www\Website2 - Copy\register.php on line 18
if (isset($_POST['submit'])){
$username = $_POST['username']; // error here
$password = md5($_POST['password']);
if(empty($username)or empty($password)){
echo"There is an empty space";
}else{
mysql_query("INSERT INTO users VALUES('', '$username', '$password')");
}
}
This happens because the empty function checks if the variables $username and $password has no value or set equal to zero. (0, null, false,''). As the empty function does not check if the variable exists, php throws this error.
In this case it would be correct to use the isset function of php.
http://br1.php.net/isset

Categories