[Solved] I am getting an undefined index error [duplicate]


You get this notice because array $_POST does not have first_name key like another ones. They are going to appeared only when you make POST request like submit the form or make AJAX call.

1. One file for GET and POST request.

In your snippet I can see you have one *.php file for GET method and POST method. To avoid this situation you can make two different files: indexPost.php and index.php and change HTML code:

<form action="indexPost.php" method="POST">

or make conditions to your keys:

<?php
    if (isset($_POST['first_name']) && !empty($_POST['first_name'])) {
        // Your code
    }
?>

Each key needs own conditional statement.

2. Separate files for GET and POST request

If I’m wrong about one *.php file.

Using conditional statements (look on code above) remove all listed notices. Also you can improve it to validate form and mysql query.

3. Duplicate entry for key Primary

One of your column has primary key which can’t be duplicated in one table. Let’s say the column last_name has primary key. Then in whole table you can have only one row with value LoremIpsum in last_name column.

solved I am getting an undefined index error [duplicate]