\n"; if (array_key_exists('submit', $_POST)) { // submit button pressed submit_employee($db); get_employees($db); } else if ($_POST['update']) { // update button pressed get_employees($db); } else if ($_POST['deralph']) { // de_ralph button pressed de_ralph($db); get_employees($db); } else { // no button pressed; initial arrival get_employees($db); } printform(); // done in all cases print ""; // end of main program function submit_employee($db) { // extract employee names from the fields POSTed print ("starting submit_employee()
\n"); $fname= $_POST['fname']; $minit= $_POST['minit']; $lname= $_POST['lname']; $ssn = $_POST['ssn']; $bdate= $_POST['bdate']; $address=$_POST['address']; $sex = $_POST['sex']; $salary=$_POST['salary']; $super_ssn=$_POST['super_ssn']; $dno = $_POST['dno']; //validate($fname, ...., $dno); // placehoder // diagnostics print htmlspecialchars("inserting record: lname=$lname, fname=$fname, ssn=$ssn, fssn=$fssn") . "

"; $insertion="insert into EMPLOYEE values (?,?,?,?,?,?,?,?,?,?)"; $types = array('text', 'text', 'text', 'text', 'text', 'text', 'text', 'decimal', // salary 'text', 'integer'); // dept number // $stmt = $db->prepare($insertion, $types, MDB2_PREPARE_MANIP); // MDB2 version $stmt = $db->prepare($insertion); if ($stmt == FALSE) { print("bad prepared statement:" . $stmt->getMessage()); die(); } $queryargs = array($fname, $minit, $lname, $ssn, $bdate, $address, $sex, $salary, $super_ssn, $dno); // alternative MDB2 way of doing this //$stmt->bindValueArray($queryargs); //print "the query object to be executed:

"; print_r($stmt); //$ires = $stmt->execute(); $ret = $stmt->execute($queryargs); if ($ret == FALSE) { print("insertion not successful: "); $fail=1; } else { print "record was inserted

"; $stmt->closeCursor(); } } // despite the name, get_employees() also prints the employee table function get_employees($db) { $query="select e.fname, e.minit, e.lname, e.ssn, e.sex, e.bdate, e.salary, concat(s.fname, ' ', s.lname) as supervisor, concat(d.dnumber, ' (', d.dname, ')' ) as dept from (employee e left join employee s on e.super_ssn = s.ssn) left outer join department d on e.dno = d.dnumber"; $qstmt = $db->prepare($query); // , array(), MDB2_PREPARE_RESULT); $qstmt->execute(); if ($qstmt == FALSE) { die("query not successful: "); } print "

Table of Employees

"; table_format_pdo($qstmt); print "

"; } // remove ralph function de_ralph($db) { $query = "delete from employee where fname = 'ralph'"; $db->query($query); } function printform() { print << Use this page to enter new employees

required

required
M/F
YYYY-MM-DD
(annual salary)
supervisor (by SSN)
address
department number

      FORMEND; makePageButtons(); } ?>