A file named add_one_employee.php displays the input fields of the record to be added to the database using an HTML form. When the user clicks on Submit, the form is submitted to the add_employee.php script, which will take that information and add it to the database, giving it a new unique employee number.
add_one_employee.php is primarily an HTML form. Unless we wanted to fill in some of its values with information from the database, it doesn't need to reference the database. An example of when you might want to fill in the information from the database is when you want to assign the employee to an existing department, and allow the user to select the department rather than typing in a department name. But we won't do that here.
<form method="post" action="add_employee.php"> <ul> <li>Last Name: <input type="text" name="lastname"> <li>First Name: <input type="text" name="firstname"> <li>Birth Date: <input type="text" name="birthdate"> <li>Admin: <input type="radio" name="isadmin" value="yes">Yes <input type="radio" name="isadmin" value="no" checked>No </ul> <input type="submit" value="Submit"> </form>
Now to actually add the employee using add_employee.php to process the input, create a new employee number, and add the employee to the table:
<?php function dquote($str){ return str_replace("'","''",$str); } $db = odbc_connect("iaidb", "css_test", "password") or die ("could not connect<br />"); $ln = dquote($_POST["lastname"]); $fn = dquote($_POST["firstname"]); $bd = date("Ymd", strtotime($_POST["birthdate"])); $adm = $_POST["isadmin"]; // either "yes" or "no" $ad = 0; if ($adm == "yes") $ad = 1; $find_stmt = "select max(EmployeeNumber) from tblEmployees"; $result = odbc_exec($db, $find_stmt); $id = odbc_result($result, 1) + 1; odbc_free_result($result); $insert_stmt = "insert into tblEmployees (EmployeeNumber, LastName, FirstName, BirthDate, IsAdmin) ". "values($id, '$ln', '$fn', '$bd', $ad)"; $result = odbc_exec($db, $insert_stmt); if ($result == FALSE) die("<br />Could not execute statement ".$insert_stmt); odbc_free_result($result); odbc_close($db); include 'showEditableEmps.php'; ?>