Notice: Undefined index: $username [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 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

Related

How to solve this error Undefined index: password in on line [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 last year.
Undefined index: password in on line
: Cannot modify header information - headers already sent by (output started at /storage/ssd4/628/18415628/public_html/Kunj/admin/index.php:8) in on line
: Cannot modify header information - headers already sent by (output started at /storage/ssd4/628/18415628/public_html/Kunj/admin/index.php:8) in on line
Not Access!
This error comes from this code
<?php
session_start();
require_once('functions.php');
require_once('dbconnection.php');
require_once('includes/init.php');
$password = $_SESSION['password'];
$admin_data = get_admin($conn, $password);
if ($password != $admin_data['password']) {
header('location: login.php');
Change this line
$password = $_SESSION['password'];
to
if(isset($_SESSION['password'])){
$password = $_SESSION['password'];
}else{
$password = '';
}

Undefined index IN PHP SESSION? [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)
The 3 different equals
(5 answers)
Closed 2 years ago.
I have this code in my login.hmtl in s section with ajax method:
$.ajax ({
type:'post',
url:'Controladores/login.php',
data:{do_login:"do_login", nick:nick, pass:pass },
success:function(response) {
alert($.trim(response));
if ($.trim(response) ==='Correcto'){
window.location.href="intranet.php?ruta=inicio";
return false;
}
else{
alert('Datos incorrectos. Volver a intentar.');
}
}
});
This redirects me to login.php where I connect to the database and then check if the username and password are correct. If they are, they return an answer Correct or Incorrect.
Here is the main part where I start my session, and also if username and password match, set the username 'NICK' to the session.
session_start();
$db = new Conexion();
$conn = $db->connect( '00.00.00.00', 'Seguridad');
$param = [$nick, $pass];
$query = "exec Seguridad.dbo.autenticacion ?, ?";
$result= $db->rows($conn, $query, $param);
$obj = json_decode($result);
//echo $obj[0]->resultado;
if( $obj[0]->resultado ='Correcto' ){
$_SESSION['nick']= $nick;
$_SESSION['nombreusuario'] = $obj[0]->nombreusuario;
$_SESSION['agencia'] = $obj[0]->CodigoAgencia;
echo ("Correcto");
}else{
if ($obj[0]->resultado =='Incorrecto'){
echo ("Incorrecto");
}
}
Then, In my home page called inicio.php I called again the session but the message is:
Notice: Undefined index: nick in C:\xampp\htdocs\intranetv3\Vistas\modulos\inicio.php on line 3
<?php
session_start();
$userName = $_SESSION['nick'];
echo "$userName";
?>
I have read other article but have not found anything.
I checked if the answer Correct or Incorrect returns the answer and it returns.
I printed the user name and password. It worked.
I also printed the $_SESSION['nick'], I see it has the username.
Apparently when I go to my inicio.hmtl the session just unssets or something, I have no idea what might be wrong. Please HELP!!

How to solve "Notice: Undefined index: name in /var/www/html/Mini_Project/php/feedback_script.php" while inserting in form in mysql 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 3 years ago.
Actually, I'm doing mini project on "faculty feedback system".
Following is the code that inserts the form entries into the database.
include.php file is for database connectivity
<?php
require "../includes/include.php";
$name=$_POST['name'];
if($name="")
echo "Name field can't be empty !!!<br>";
$faculty=$_POST['faculty'];
if($faculty="")
echo "Please select your faculty !!!<br>";
$rating=$_POST['rating'];
if($rating="")
echo "Please select the respective rating !!!<br>";
$response=$_POST['response'];
$name=mysqli_real_escape_string($con, $name);
$faculty=mysqli_real_escape_string($con, $faculty);
$rating=mysqli_real_escape_string($con, $rating);
$response=mysqli_real_escape_string($con, $response);
$insert_query="insert into feedback(name, faculty, rating, response) values ('$name', '$faculty', '$rating', '$response')";
$query_result=mysqli_query($con, $insert_query);
?>
It shows the error while inserting values
Notice: Undefined index: name in /var/www/html/Mini_Project/php/feedback_script.php on line 12
.
Please solve this error
Your assignment of $name, $faculty, $rating, and $response expects your $_POST array to have certain keys in it. If you cannot garantee your $_POST to have all that, it's best to use the null coalesce operator (i.e. double question mark ??) to define a fallback value for assignment:
$name=$_POST['name'] ?? "";
if($name="")
echo "Name field can't be empty !!!<br>";
$faculty=$_POST['faculty'] ?? "";
if($faculty="")
echo "Please select your faculty !!!<br>";
$rating=$_POST['rating'] ?? "";
if($rating="")
echo "Please select the respective rating !!!<br>";
$response=$_POST['response'] ?? "";

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

Categories