-1

I am creating a registration page. Here is my PHP code:

    <?php
$id = $name = $pwd = $confirm_pwd = $eamil = $phone = $dob = $region = $city = "";
$idErr = $nameErr = $passErr = $phoneErr = $emailErr = $message = $dobErr = $regionErr = $cityErr = "";
if(isset($_POST['submit'])) {
  if (empty($_POST["national_id"])) {
    $nameErr = "national id is required";
  } else {
    $id = test_input($_POST["national_id"]);
  }
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
  }

  if (empty($_POST["pwd"])) {
    $passErr = "password is required";
  } else {
    $pwd = test_input($_POST["pass"]); 
  }
  /* Password Matching Validation */
if($_POST['pwd'] != $_POST['confirm_pwd']){ 
$message = 'Passwords should be same<br>'; 
}
else {
$pwd = test_input($_POST["pwd"]);   
}
 if (empty($_POST["email"])) {
    $emailErr = "email is required";
  } else {
    $email = test_input($_POST["email"]); 
  }    

  if (empty($_POST["phone"])) {
    $phoneErr = "phone is required";
  } else {
    $phone = test_input($_POST["phone"]);
  }
  if (empty($_POST["date_of_birth"])) {
    $dobErr = "date of birth is required";
  } else {
    $dob = test_input($_POST["date_of_birth"]);
  }
  if (empty($_POST["region"])) {
    $regionErr = "region is required";
  } else {
    $region = test_input($_POST["region"]);
  }
  if (empty($_POST["city"])) {
    $cityErr = "city is required";
  } else {
    $phone = test_input($_POST["city"]);
  }
}

else{
$con = new mysqli("localhost","root","","seis") or die(mysql_error());
$id = $_POST["national_id"];
$name = $_POST["name" ];
$pwd = md5($_POST["pwd" ]);
$email = $_POST["email" ];
$phone = $_POST["phone" ];
$dob = $_POST["date_of_birth" ];
$region = $_POST["region" ];
$city = $_POST["city" ];

$national_id = trim($id);
$cust_name  = trim($name);
$cust_pwd = trim($pwd);
$cust_email = trim($email);
$cust_phone = trim($phone);
$cust_dob = trim($dob);
$cust_region = trim($region);
$cust_city = trim($city);
$processvalue = "Insert INTO Registration
              VALUES ('$national_id' ,'$cust_name', '$cust_dob' ,'$cust_email' , '$cust_pwd' , '$phone' , (select region_serial,city_serial from cities where region = '$cust_region' AND city = '$cust_city') )";


if (mysqli_query($con, $processvalue)) {

echo 'You are registered ';

} else {
   echo "error:" .mysqli_error($con);
}
}
mysqli_close($con)

?>

I am trying to insert each variable to an SQL statement, but it seems like I have a problem with variable initialization.

This is the error I get:

Notice: Undefined index: national_id in C:\xampp\htdocs\seis-new\signup.php on line 68

Notice: Undefined index: name in C:\xampp\htdocs\seis-new\signup.php on line 69

Notice: Undefined index: pwd in C:\xampp\htdocs\seis-new\signup.php on line 70

Notice: Undefined index: email in C:\xampp\htdocs\seis-new\signup.php on line 71

Notice: Undefined index: phone in C:\xampp\htdocs\seis-new\signup.php on line 72

Notice: Undefined index: date_of_birth in C:\xampp\htdocs\seis-new\signup.php on line 73

Notice: Undefined index: region in C:\xampp\htdocs\seis-new\signup.php on line 74

Notice: Undefined index: city in C:\xampp\htdocs\seis-new\signup.php on line 75 error:Table 'seis.registration' doesn't exist

TRiG
  • 10,148
  • 7
  • 57
  • 107
younis
  • 71
  • 1
  • 5
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Jens May 16 '17 at 10:31
  • learn about prepared Statements, to prevent SQL-injection – Jens May 16 '17 at 10:32
  • 1
    You mix `mysql_` and `mysqli_` API *or die(mysql_error());* – Jens May 16 '17 at 10:33

4 Answers4

0

you have to use isset() function to check variable index is set or not

Amit Gaud
  • 756
  • 6
  • 15
0

function empty() check value. If you will give assoc array, then you need to have given key.

Check first, if value exist in given key by isset().

Simply check, if this request is not good practice.

Your IF statement should look like:

$errors = [];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (!isset($_POST["national_id"]) || empty($_POST["national_id"])) {
        // didn't provide field national_id or is empty
        $errors[] = "national id is required";
    } else {
        $id = test_input($_POST["national_id"]);
    }
}

if (count($errors)) {
    // You have errors
}
timiTao
  • 1,417
  • 3
  • 20
  • 34
  • it saying that the variables i used is unidentified :$id = $_POST["national_id"]; $name = $_POST["name" ]; $pwd = md5($_POST["pwd" ]); $email = $_POST["email" ]; $phone = $_POST["phone" ]; $dob = $_POST["date_of_birth" ]; $region = $_POST["region" ]; $city = $_POST["city" ]; – younis May 16 '17 at 11:38
  • of course, i gave you only example of 1 field. You need to update you whole code - all ifs that way. See the example of `isset()` with negation. If your code will have empty array `$errors`, then you can use your variables without problems. I only give you idea, how to improve code. – timiTao May 16 '17 at 11:42
0

First of all what have you done on 2nd and 3rd line too many variables equal? Can you please tell whats you intention doing so...

The reason you are getting undefined indexes because you have problem in your form. Check all the name attributes in your form and use that names as indexes in your $_POST['index'] method....

Saqlain
  • 465
  • 3
  • 11
0

On your first line, you mispelled the variable you wrote $eamil and every other line is written $email fix this and check if it works, it probably will.

I recommend you to write your IF statements to check for empty fields like this:

  if (!empty($_POST["national_id"])) {
    $id = test_input($_POST["national_id"]);
  } else {
    $nameErr = "national id is required";
  }

Also, your $convariable is connecting to the database using mysqli but then you wrote mysql_error. You must use mysqliall the time, otherwise it don't detect the table you wish. And I would take the comments advices to improve the entire code, it's a bit messy and there's a lot of stuff than can retrieve errors.

Obs: Reputation lower than 50, cannot comment yet.