Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Undefined array key when post data in PHP

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading