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);
?>