lis-458-ol-php/employee.php

114 lines
3.6 KiB
PHP
Raw Permalink Normal View History

2018-02-18 01:28:11 +00:00
<?php # Script 9.5 - register.php #2
// This script performs an INSERT query to add a record to the users table.
$page_title = 'New Lecturer';
include ('includes/header.html');
// Check for form submission:
// echo $_SERVER['REQUEST_METHOD'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//echo '1';
require('mysqli_connect.php'); // Connect to the db.
//echo '2';
$errors = array(); // Initialize an error array.
//echo '3';
// Check for a first name:
if (empty($_POST['first_name'])) {
echo 'You forgot to enter your first name.';
$errors[] = 'You forgot to enter your first name.';
} else {
$fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
}
// Check for a last name:
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$ln = mysqli_real_escape_string($dbc, trim($_POST['last_name']));
}
// Check for an email address:
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = mysqli_real_escape_string($dbc, trim($_POST['email']));
}
if (empty($_POST['gender'])) {
$errors[] = 'You forgot to enter your gender.';
} else {
$g = mysqli_real_escape_string($dbc, trim($_POST['gender']));
}
if (empty($_POST['ssn'])) {
$errors[] = 'You forgot to enter your ssn.';
} else {
$s = mysqli_real_escape_string($dbc, trim($_POST['ssn']));
}
if (empty($errors)) { // If everything's OK.
// Register the lecturer in the database...
// Make the query:
$q = "INSERT INTO Lecturers (LecLastName, LecFirstName, LecEmail, Gender, SSN) VALUES ('$ln', '$fn', '$e', '$g', '$s')";
$r = @mysqli_query ($dbc, $q); // Run the query.
if ($r) { // If it ran OK.
// Print a message:
echo '<h1>Thank you!</h1>
<p>You are now in the database.</p><p><br /></p>';
} else { // If it did not run OK.
// Public message:
echo '<h1>System Error</h1>
<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</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.
// Include the footer and quit the script:
include ('includes/footer.html');
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.
2018-02-18 01:30:12 +00:00
?>
<h1>Register</h1>
<form action="employee.php" method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
<p>Email Address: <input type="text" name="email" size="20" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>
<p>Gender: <input type="text" name="gender" size="10" maxlength="20" value="<?php if (isset($_POST['gender'])) echo $_POST['gender']; ?>" /></p>
<p>SSN: <input type="text" name="ssn" size="10" maxlength="20" value="<?php if (isset($_POST['ssn'])) echo $_POST['ssn']; ?>" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
</form>
2018-02-18 01:28:11 +00:00
<?php include ('includes/footer.html'); ?>