Home > front end >  Undefined array key when post data in PHP
Undefined array key when post data in PHP

Time:12-19

I have a Database with 3 tables, in this error I'm only using table Customers.

Table Customers has 4 rows: id_customer, fn_customer, ln_customer and email_customer.

I'm making a form in file InsertCustomers.php so I can insert data in this table. The problem is when I click to submit data this error appears: Undefined array key.

This errors appears in this 3 lines:

$input_fn = trim($_POST["fn_customer"]);
$input_ln = trim($_POST["ln_customer"]);
$input_email = trim($_POST["email_customer"]);

This is the a portion of the code:

// Include config file
require_once "../connectDB.php";
 
// Define variables and initialize with empty values
$fn_customer = $ln_customer = $email_customer = "";
$fn_customer_err = $ln_customer_err = $email_customer_err = "";

// Processing form data when form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate name
    $input_fn = trim($_POST["fn_customer"]);
    if (empty($input_fn)) {
        $fn_err = "Please enter a name.";
    } elseif (!filter_var($input_fn, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[a-zA-Z\s] $/")))) {
        $fn_err = "Please enter a valid name.";
    } else {
        $fn_customer = $input_fn;
    }

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Validate name
        $input_ln = trim($_POST["ln_customer"]);
        if (empty($input_ln)) {
            $ln_err = "Please enter a name.";
        } elseif (!filter_var($input_ln, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[a-zA-Z\s] $/")))) {
            $ln_err = "Please enter a valid name.";
        } else {
            $ln_customer = $input_ln;
        }
    }

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Validate name
        $input_email = trim($_POST["email_customer"]);
        if (empty($input_email)) {
            $email_err = "Please enter a email.";
        } else {
            $email_customer = $input_ln;
        }
    }
...

I tried var_dumping and trying to isset get this data, but it didn't work.

CodePudding user response:

Probably fn_customer, in_customer and email_customer index do not exist on $_POST var.

You can validate then using isset, for example:

if(isset($_POST['desired_index')) {
   // Your code
} else {
  // An error
}

This solve the form validation.

I just saw your code in source. The problem is in the name of the inputs. Just rename then like this: fn_customer -> fn, ln_customer -> ln, email_customer -> email

CodePudding user response:

Your variable names seem to differ from the form names:

<input type="text" name="fn" ...

This will result in $_POST["fn"], not in $_POST["fn_customer"]. Same goes for ln and email. Use the same names and you are good to go.

Edit: Check comment by Cole

CodePudding user response:

In the file connectDB.php you have to specify the Table Customers,

$fn_customer = $ln_customer = $email_customer = ""; $fn_customer_err = $ln_customer_err = $email_customer_err = "";

When bringing the id variables from the table you are leaving the value of the null arrays.

Instead of equaling them to null you must add variables to the arrays, the generation of new ids is automatic, and to change them you must specify the position

you still need to make a form to send the data:

<form action="" method="post">

"button to send"<input type="submit" name="Submit">

</form>
  • Related