The form almost works correctly. A user gets added but 0 is put in the position_id field of the employees table. The form is not inserting the position_id correctly from the combo box. Here is the code:
$hostname="localhost" ; // Machine on which MySQL database is running
$dbusername="webuser" ; // Database user login
$databaseName="ditdb" ; // Database name
$db = mysql_connect($hostname, $dbusername);
mysql_select_db($databaseName, $db);
$result=mysql_query("SELECT * FROM position ORDER BY position", $db);
if ($submit) {
Add_Employee($first,$last,$extension,$pager,$cellphone,$homephone,$position_id);
if ($feedback) {
echo '<FONT COLOR="RED"><H2>'.$feedback.'</H2></FONT>';
}
} else {
echo "<FORM ACTION='$PHP_SELF' METHOD='POST'>";
$XX = "data empty!";
?>
<P>First Name: <? echo "<INPUT TYPE='TEXT' NAME='first' VALUE='$first' SIZE='20' MAXLENGTH='20'></P>\n"; ?>
<P>Last Name: <? echo "<INPUT TYPE='TEXT' NAME='last' VALUE='$last' SIZE='20' MAXLENGTH='20'></P>\n"; ?>
<P>Position:
<?
echo "<select name=$position_id>";
while($myrow=mysql_fetch_array($result)) {
$position_id = $myrow['position_id'];
$position = $myrow['position'];
echo "<option value=$position_id>$position\n";
}
if ($position_id="")
{
print("$XX");
}
echo "</select><P>\n";
echo "<INPUT TYPE='SUBMIT' NAME='submit' VALUE='Submit Employee Information'>";
echo "</FORM>\n";
Here's the code for Add_Employees:
// Add an employee
function Add_Employee($first,$last,$extension,$pager,$cellphone,$homephone,$position_id) {
global $feedback;
// does the employee exist in the database?
$sql="SELECT * FROM employees WHERE first='$first' && last='$last'";
$result=db_query($sql);
if ($result && db_numrows($result) > 0) {
$feedback .= ' ERROR - EMPLOYEE EXISTS ';
return false;
} else {
// add a new employee
$sql = "INSERT INTO employees (first, last, position) VALUES ('$first', '$last', '$position_id')";
$result=db_query($sql);
if (!$result) {
$feedback .= ' ERROR - '.db_error();
return false;
} else {
$feedback .= ' Successfully added Employee. ';
return true;
}
}
}