This is my first useful script. It is designed to take employee data and store it in a simple database for later retrieval.
The script seems to work fine until it checks all of the feild variables. It recognizes that there is data entered into the fields, but still escapes to the error message. I have provided incase it finds that any of the variables are undeclared..
This is the relavent code.
if (isset($_POST['submit'])) { // handling the form
// register the user in the database
require_once ('mysql_connect.php'); // Connection to DB
// Create a function for escaping the data.
function escape_data ($data) {
global $dbc; // Ensures the connection
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string($data, $dbc);
} // Strip slashing is done
$message = NULL; // declaring the error message variable.
// Check for first name.
if (empty($_POST['fname'])) {
$fn = FALSE;
$message .= '<p>You forgot to enter the employees first name.</p>';
} else {
$fn = escape_data($_POST['fname']);
}
// Check for last name.
if (empty($_POST['lname'])) {
$ln = FALSE;
$message .= '<p>You forgot to enter the employees last name.</p>';
} else {
$ln = escape_data($_POST['lname']);
}
// Check for rank.
if (empty($_POST['rank'])) {
$r = FALSE;
$message .= '<p>You forgot to enter the employees rank.</p>';
} else {
$r = escape_data($_POST['rank']);
}
// Check for phone number.
If (empty($_POST['phone'])) {
$ph = FALSE;
$message .= '<p>You forgot to enter the employees phone number.</p>';
} else {
$ph = escape_data($_POST['phone']);
}
// Check for e-mail address.
if (empty($_POST['email'])) {
$em = FALSE;
$message .= '<p>You forgot to enter the employees E-mail address.</p>';
} else {
$em = escape_data($_POST['email']);
}
if ($fn && $ln && $r && $ph && $em) { // If all the fields have been completed
$query = "SELECT empnum FROM jnet_project1 WHERE fname='$fn' AND lname='$ln'";
$result = @mysql_query ($query); // This line executes the query for copy check
if (mysql_num_rows($result) == 0) {
// Make the main insert
$query = "INSERT INTO jnet_project1 (fname, lname, rank, phone, email) VALUES ('$fn', '$ln', '$r', '$ph', '$em')";
$result = @mysql_query ($query); // Execute the insert
if ($result) { // If it ran OK... do...
echo '<p><b>The employee information has been entered.</b></p>';
include ('footer.inc'); // Include footer
exit(); // quit the script.
} else { // If the insert failed
$message = '<p>There was an error when entering the employee data.</p><p>' . mysql_error() . '</p>';
}
} else {
$message = '<p>That employee information is already in our system.</p>';
}
mysql_close(); // close the mysql connection.
} else {
$message .= '<p>Please try again.</p>';
}
} // End of the conditionals
This is the uploaded version of the site (ignore the cragged layout, I know why, just havent dealt with it yet)
http://www.jasconius.net/registry.php
The solution is beyond me.
(edit) The error message is "Please try again" the overall conditional else.