This code is to create a simple database to insert records into a table ...
but the table appears to be empty with the following errors after entry>>

errors:
Notice: Undefined index: FirstName in C:\xampp\htdocs\my_php_files\insert.php on line 34

Notice: Undefined index: LastName in C:\xampp\htdocs\my_php_files\insert.php on line 35

Notice: Undefined index: Age in C:\xampp\htdocs\my_php_files\insert.php on line 36

 <?php

$con = mysqli_connect("localhost","root","distortion","my_databs");

// Check connection

if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


// Create table

$sql = "CREATE TABLE IF NOT EXISTS Employee(PID INT NOT NULL AUTO_INCREMENT , 
                    FirstName    CHAR(30),
                    LastName     CHAR(30),
                    Age          INT(5),
                    PRIMARY KEY  (PID)
                    )";
echo " <br> ";

// Execute query

if (mysqli_query($con,$sql)) {
echo " Table Created Successfully ";
} else {
 echo " Error creating table: " . mysqli_error($con);
}


// escape variables for security

$firstname  = mysqli_real_escape_string($con, $_POST['FirstName']);
$lastname   = mysqli_real_escape_string($con, $_POST['LastName']);
$age        = mysqli_real_escape_string($con, $_POST['Age']);


$sql="INSERT INTO Employee (FirstName, LastName, Age ) 
       VALUES ('$firstname', '$lastname', '$age')";


  echo "<br>";
if (!mysqli_query($con,$sql)) {

 die( 'Error:' . mysqli_error($con));

 }
echo"<br>";
echo " -> 1 record added";



 // Display In A Table

 $result = mysqli_query( $con , " SELECT * FROM Employee " );

echo "<table border='5'> <tr> <th> PID       </th> <th> First Name </th> <th> Last Name  </th> <th> Age       </th> </tr>";



while($row = mysqli_fetch_array($result)) {
   echo "<tr>";

  echo "<td>" . $row['PID']       .  "</td>";
  echo "<td>" . $row['FirstName'] .  "</td>";
  echo "<td>" . $row['LastName']  .  "</td>";
  echo "<td>" . $row['Age']       .  "</td>";

 echo ("<td><a href=\"edit_form.php?id=$row[PID]\">Edit</a></td>");
 echo ("<td><a href=\"delete_form.php?id=$row[PID]\">Delete</a></td>");

 echo "</tr>";
 }  


 echo "</table>";


mysqli_close($con);

?> 

Forget this - need to re-analyze the original code.

Reasons:

1) Your html part is not having LastName , Age elements properlly spelled
2) Your input method may be GET instead of POST

@urtrivedi may i know which line is mispelled.

Post your html code also

<!DOCTYPE html>
<html>
<body>
<style>
.error {color: #FF0000;}
form label{
display: inline-block;

</style>


 <div id= "container" style "width:500px">
< div  style="background:#80BFFF">
<div id="header" style="background-color:#FFA500;">
 <h1 style="margin-bottom:0;">
 <h1  style="text-align:center;"><u>Employee Details</u></h1></h1></div>

<p><span class = "error">* Required Field. </span></p>
 <form action="insert.php" method="post">
<h3 style="text-align:center;">

<label for="Firstname">First Name:</label>      <input type="text"     name="firstname" /><br/> 
<label for="Lastname">Last Name:</label>        <input type="text" name="lastname" /><br/> 
<label for="Age">Age:</label>                   <input type="text" name="age" /><br/>
<br class="clear" />
</form>

<br>  <form action="insert.php" method = "post">
    <input type="submit" value = "Save">
  </form>
  <form action= "Menu.php" method= "POST">
  <input type ="submit" value = "Cancel">
  </form>

<br>

 <form action= "delete.php" method= "POST">
<br><input type ="submit" value = "Delete All Records">
 </form></h3>



 </div>
</body>
 </html>

if you see line 22, 23, 24

all input names are in small letters
firstname
lastname
age

and in your php handler code in your first post its in title case. Line 33,34,35

Firstname
Lastmame
Age

Both case must match as php is case sensitive like C language

I'm sorry .. but i'm getting the same error as before

i'm afraid im getting the same error

remove line 26 to 28 in your html code, your html elements and submit button must be under same form element. You have ended form early

</form>

<br>  <form action="insert.php" method = "post">

Your insert form must look like following

<form action="insert.php" method="post">
<h3 style="text-align:center;">

<label for="Firstname">First Name:</label>      <input type="text"     name="Firstname" /><br/> 
<label for="Lastname">Last Name:</label>        <input type="text" name="Lastname" /><br/> 
<label for="Age">Age:</label>                   <input type="text" name="Age" /><br/>
<br class="clear" />

<input type="submit" value = "Save">
  </form>

nothing seems to work..
i dont think it has anything to do with the HTML code.

what urtivedi said.

this is for insert yes?

$firstname  = mysqli_real_escape_string($con, $_POST['FirstName']);
$lastname   = mysqli_real_escape_string($con, $_POST['LastName']);
$age        = mysqli_real_escape_string($con, $_POST['Age']);

but in you html:

<input type="text" name="firstname" /><br/> 
<input type="text" name="lastname" /><br/> 
<input type="text" name="age" /><br/>

you name are all small letters. they should be FirstName LastName and Age or you change the insert to small letters.

as in:

$firstname  = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname   = mysqli_real_escape_string($con, $_POST['lastname']);
$age        = mysqli_real_escape_string($con, $_POST['age']);

also he mentioned ur form tags. make sure all input is in between the same
<form> </form>
meaning the input for first name, last name and age AND the save button.

technically right now you have this

<form>
    <input type="text" name="firstname" /><br/> 
    <input type="text" name="lastname" /><br/> 
    <input type="text" name="age" /><br/>
</form>

<form>
    <input type="submit" name="save" value="Save">
</form>

When it should be:

<form method="post" action="insert.php">
    <input type="text" name="firstname" /><br/> 
    <input type="text" name="lastname" /><br/> 
    <input type="text" name="age" /><br/>
    <input type="submit" name="save" value="Save">
</form>

also use PDO. i feel kinda weird saying that coz i was told that a million times on forums before i actually did migrate to pdo. it may look confusing and all but u will get the hang of it soon. and i actually find it much easier than mysql_/mysqli_ functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.