Saturday 14 September 2019

html - PHP - Undefined index error: email and password on register form




I'm very new guy on php. I have a website project on my course. I'm stuck on this part and could not move more. Need to help for solving this. I tried to reach email and password like on my code, this error says it's not like this.



I have an error like this:





Notice: Undefined index: email in C:\xampp\htdocs\CONFIG.PHP on line 6
Notice: Undefined index: password in C:\xampp\htdocs\CONFIG.PHP on
line 7 Info could not write on database!




(solved with



if($_SERVER['REQUEST_METHOD'] == 'POST') {
//...



) but now any info does not go to database



config.php:



    ob_start();
include "dbconnect.php";


$email=$_POST["email"];
$password=$_POST["password"];
$username=$_POST["username"];
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
$sql="select * from users where email = '".$email."' and password = '".$password."' and username = '".$username."'";
$query=mysql_query($sql);
$counter=mysql_num_rows($query);
if ($counter!=0){
echo "This user has been registered before. Please check information.";
}else{

$sql2 = "INSERT INTO users(email, password, username) VALUES ('".$email."','".$password."','".$username."'";
$add = mysql_query($sql2);
if($submit){
echo "
You have registered successfully!
Please click. ";
header("Location:regedIndex.html");
}else{
echo "Info could not write on database!";

}

ob_end_flush();
}
?>


register.html:













Answer



Add if($_SERVER['REQUEST_METHOD'] == 'POST') to check if the form has been submitted.



e.g.



  if($_SERVER['REQUEST_METHOD'] == 'POST') {
ob_start();

include "dbconnect.php";

$email=$_POST["email"];
$password=$_POST["password"];
$username=$_POST["username"];
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
$sql="select * from users where email = '".$email."' and password = '".$password."' and username = '".$username."'";
$query=mysql_query($sql);
$counter=mysql_num_rows($query);
if ($counter!=0){

echo "This user has been registered before. Please check information.";
}else{
$sql2 = "INSERT INTO users(email, password, username) VALUES ('".$email."','".$password."','".$username."'";
$add = mysql_query($sql2);
if($submit){
echo "
You have registered successfully!
Please click. ";
header("Location:regedIndex.html");
}else{
echo "Info could not write on database!";


}
ob_end_flush();
}
}
?>

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...