This form throws an errors saying something about the variable in the line with VALUES not being defined. How do I fix this form with PhP to add a record to a table in my database?
<html>
<body>
<?php
$page_title = 'CreateEmployee';
// Check if the form has been submitted:
if (isset($_POST['submitted'])) {
require_once ('mysqli_connect.php'); // Connect to the db.
$errors = array(); // Initialize an error array.
// Check for a last name:
if (empty($_POST['LastName'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$fn = mysqli_real_escape_string($dbc, trim($_POST['LastName']));
}
// Check for a first name:
if (empty($_POST['FirstName'])) {
$errors[] = 'You forgot to enter your first name.';
} else {
$ln = mysqli_real_escape_string($dbc, trim($_POST['FirstName']));
}
// Check for a Title:
if (empty($_POST['title'])) {
$errors[] = 'You forgot to enter your title.';
} else {
$e = mysqli_real_escape_string($dbc, trim($_POST['title']));
}
// Check for a Title:
if (empty($_POST['city'])) {
$errors[] = 'You forgot to enter your city.';
} else {
$e = mysqli_real_escape_string($dbc, trim($_POST['city']));
}
if (empty($errors)) { // If everything's OK.
// Register the user in the database...
// Make the query:
$q = "INSERT INTO employees (LastName, FirstName, title, city) VALUES ('$LastName', '$FirstName', '$Title', '$City', NOW() )";
$r = @mysqli_query ($dbc, $q); // Run the query.
if ($r) { // If it ran OK.
// Print a message:
echo '<h1>Thank you!</h1>
<p>You have now created a New Employee!</p><p><br /></p>';
} else { // If it did not run OK.
// Public message:
echo '<h1>System Error</h1>
<p class="error">You failed to create a new employee.</p>';
// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
} // End of if ($r) IF.
mysqli_close($dbc); // Close the database connection.
// Exit the script
exit();
} else { // Report the errors.
echo '<h1>Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
} // End of if (empty($errors)) IF.
mysqli_close($dbc); // Close the database connection.
} // End of the main Submit conditional.
?>
<h1>Add Employee</h1>
<form action="CreateEmployee.php" method="post">
<p>Last Name: <input type="text" name="LastName" size="15" maxlength="20" value="<?php if (isset($_POST['LastName'])) echo $_POST['LastName']; ?>" /></p>
<p>First Name: <input type="text" name="FirstName" size="15" maxlength="40" value="<?php if (isset($_POST['FirstName'])) echo $_POST['FirstName']; ?>" /></p>
<p>Title: <input type="text" name="title" size="20" maxlength="80" value="<?php if (isset($_POST['title'])) echo $_POST['title']; ?>" /> </p>
<p>City: <input type="city" name="city" size="10" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Create Employee" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
<html>
<body>
<?php
$page_title = 'CreateEmployee';
// Check if the form has been submitted:
if (isset($_POST['submitted'])) {
require_once ('mysqli_connect.php'); // Connect to the db.
$errors = array(); // Initialize an error array.
// Check for a last name:
if (empty($_POST['LastName'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$fn = mysqli_real_escape_string($dbc, trim($_POST['LastName']));
}
// Check for a first name:
if (empty($_POST['FirstName'])) {
$errors[] = 'You forgot to enter your first name.';
} else {
$ln = mysqli_real_escape_string($dbc, trim($_POST['FirstName']));
}
// Check for a Title:
if (empty($_POST['title'])) {
$errors[] = 'You forgot to enter your title.';
} else {
$e = mysqli_real_escape_string($dbc, trim($_POST['title']));
}
// Check for a Title:
if (empty($_POST['city'])) {
$errors[] = 'You forgot to enter your city.';
} else {
$e = mysqli_real_escape_string($dbc, trim($_POST['city']));
}
if (empty($errors)) { // If everything's OK.
// Register the user in the database...
// Make the query:
$q = "INSERT INTO employees (LastName, FirstName, title, city) VALUES ('$LastName', '$FirstName', '$Title', '$City', NOW() )";
$r = @mysqli_query ($dbc, $q); // Run the query.
if ($r) { // If it ran OK.
// Print a message:
echo '<h1>Thank you!</h1>
<p>You have now created a New Employee!</p><p><br /></p>';
} else { // If it did not run OK.
// Public message:
echo '<h1>System Error</h1>
<p class="error">You failed to create a new employee.</p>';
// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
} // End of if ($r) IF.
mysqli_close($dbc); // Close the database connection.
// Exit the script
exit();
} else { // Report the errors.
echo '<h1>Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
} // End of if (empty($errors)) IF.
mysqli_close($dbc); // Close the database connection.
} // End of the main Submit conditional.
?>
<h1>Add Employee</h1>
<form action="CreateEmployee.php" method="post">
<p>Last Name: <input type="text" name="LastName" size="15" maxlength="20" value="<?php if (isset($_POST['LastName'])) echo $_POST['LastName']; ?>" /></p>
<p>First Name: <input type="text" name="FirstName" size="15" maxlength="40" value="<?php if (isset($_POST['FirstName'])) echo $_POST['FirstName']; ?>" /></p>
<p>Title: <input type="text" name="title" size="20" maxlength="80" value="<?php if (isset($_POST['title'])) echo $_POST['title']; ?>" /> </p>
<p>City: <input type="city" name="city" size="10" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Create Employee" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>