Hi, can any tell me what is going wrong I have created a script to display a form to enter data into the database the script should display an entry form and checks for valid inputs then submit valid data to the DB.
The script runs without errors and displays the final message "New Staff Member added to database" as soon as the page is loaded, suggesting that everything has gone ok but the form was never displayed and no data entered.
Any ideas? Sorry to post a load of code but the script is show below, I think im missing something simple. I have changed the password and user, there are no issues connecting to the database when i tested and ouput script to show data entred through mysql console.
Thanks in advance for any help you can give
/* Program name: staff_entry.php
* Description: Program checks all the form fields for
* blank fields and incorrect format. Saves the
* correct fields in a database.
*/
?>
<html>
<head><title>Staff Details Entry</title></head>
<body>
<?php
$staffid = strip_tags(trim($_POST['phone']));
$firstname = strip_tags(trim($_POST['firstname']));
$lastname = strip_tags(trim($_POST['lastname']));
$department = strip_tags(trim($_POST['department']));
$roomnumber = strip_tags(trim($_POST['roomnumber']));
$phonenumber = strip_tags(trim($_POST['phonenumber']));
/* check information from the form */
/* set up array of field labels */
$label_array = array ( "staffid" => "Staff ID",
"firstname" => "First Name",
"lastname" => "Last Name",
"department" => "Department",
"roomnumber" => "Room Number",
"phonenumber" => "Phone Number"
;
foreach ($_POST as $field => $value)
{
/* check each field for blank fields */
if ( $value == "" )
{
$blank_array[$field] = "blank";
}
elseif ( ereg("(name)",$field) )
{
if (!ereg("^[A-Za-z' -]{1,50}$",$_POST[$field]) )
{
$bad_format[$field] = "bad";
}
}
elseif ($field == "phonenumber"
{
if(!ereg("^[0-9)( -]{7,20}(([xX]|(ext)|(ex))?[ -]?[0-9]{1,7})?$",$value) )
{
$bad_format[$field] = "bad";
}
}
} // end of foreach for $_POST
/* if any fields were not okay, display error message and form */
if (@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0)
{
if (@sizeof($blank_array) > 0)
{
/* display message for missing information */
echo "<b>You didn't fill in one or more required fields.
You must enter:</b><br>";
/* display list of missing information */
foreach($blank_array as $field => $value)
{
echo " {$label_array[$field]}<br>";
}
}
if (@sizeof($bad_format) > 0)
{
/* display message for bad information */
echo "<b>One or more fields have information that appears to
be incorrect. Correct the format for:</b><br>";
/* display list of bad information */
foreach($bad_format as $field => $value)
{
echo " {$label_array[$field]}<br>";
}
}
/* redisplay form */
echo "<p><hr>
<form action='checkAll.php' method='POST'>
<center>
<table width='95%' border='0' cellspacing='0' cellpadding='2'>
<tr><td align='right'><B>{$label_array['staffid']}:</br></td>
<td><input type='text' name='staffid' size='65' maxlength='65'
value='$staffid' > </td>
</tr>
<tr><td align='right'><B>{$label_array['firstname']}:</br></td>
<td><input type='text' name='firstname' size='65' maxlength='65'
value='$firstname' > </td>
</tr>
<tr><td align='right'><B>{$label_array['lastname']}:</B></td>
<td> <input type='text' name='lastname' size='65' maxlength='65'
value='$lastname'> </td>
</tr>
<tr><td align='right'><B>{$label_array['department']}:</B></td>
<td> <input type='text' name='department' size='65' maxlength='65'
value='$department'> </td>
</tr>
<tr><td align='right'><B>{$label_array['roomnumber']}:</B></td>
<td> <input type='text' name='roomnumber' size='65' maxlength='65'
value='$roomnumber'> </td>
</tr>
<tr><td align='right'><B>{$label_array['phonenumber']}:</B></td>
<td> <input type='text' name='phonenumber' size='65' maxlength='65'
value='$phonenumber'> </td>
</tr>
</table>
<p><input type='submit' value='staff details'>
</form>
</center>";
exit;
}
else //if data is okay
{
$user="*****";
$host="localhost";
$password="******";
$database = "**********";
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server"
;
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database"
;
$query = "INSERT INTO staff (staffid, firstname,lastname,department,roomnumber,phonenumber)
VALUES ('$staffid','$firstname','$lastname','$department','$roomnumber','$phonenumber')";
$result = mysql_query($query)
or die ("Couldn't execute query."
;
echo "New Staff Member added to database<br>";
}
?>
</body></html>