setAttribute(PDO::ATTR_EMULATE_PREPARES,false); print "
"; $insertion="insert into employee values (?,?,?,?,?,?,?,?,?,?)"; /* $types = array('text', 'text', 'text', 'text', 'text', 'text', 'text', 'decimal', // salary 'text', 'integer'); // dept number /* */ $stmt = $db->prepare($insertion); if ($stmt == FALSE) { print("failed to prepare statement: \"$insertion\"
\n"); $errarray = $db->errorInfo(); $errmsg = $errarray[2]; print("Prepare error: $errmsg
\n"); // error would live in $db die(); } $queryargs = array($fname, $minit, $lname, $ssn, $bdate, $address, $sex, $salary, $super_ssn, $dno); $ret = $stmt->execute($queryargs); if ($ret == FALSE) { print("execution of query not successful: \"$insertion\"
\n"); $errarray = $stmt->errorInfo(); $errmsg = $errarray[2]; print("Execute error: $errmsg
\n"); $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); if ($qstmt == FALSE) { print("failed to prepare statement: \"$query\"
\n"); $errarray = $db->errorInfo(); $errmsg = $errarray[2]; print("Prepare error: $errmsg
\n"); die(); } $ret = $qstmt->execute(); if ($ret == FALSE) { print("query not successful: \"$query\"
\n"); $errarray = $qstmt->errorInfo(); $errmsg = $errarray[2]; print("Execute error: $errmsg
\n"); die(); } print "
"; } // remove ralph function de_ralph($db) {de_ralph_old($db);} // de_ralph_new will only work if you've created table employeebackup. // "set foreign_key_checks=0/1" is only needed if you use innodb. // But because I do all the queries in a batch, the error-testing is broken function de_ralph_new($db) { $query1 = "set foreign_key_checks=0"; $query2 = "delete from employee"; $query3 = "insert into employee select * from employeebackup"; $query4 = "set foreign_key_checks=1"; $query = $query1 . ';' . $query2 . ';' . $query3 . ';' . $query4; $retstmt = $db->query($query); // returns FALSE on failure //$retstmt2 = $db->query($query2); //$retstmt3 = $db->query($query3); //$retstmt4 = $db->query($query4); if ($retstmt == FALSE) { print ("failed query: \"$query\"
\n"); $errarray = $db->errorInfo(); // error would live in $db $errmsg = $errarray[2]; print("Execute error: $errmsg
"); die(); } } function de_ralph_old($db) { $query = "delete from employee where fname = 'ralph'"; $retstmt = $db->query($query); // returns FALSE on failure if ($retstmt == FALSE) { print ("failed query: \"$query\"
\n"); $errarray = $db->errorInfo(); // error would live in $db $errmsg = $errarray[2]; print("Execute error: $errmsg
");
die();
}
}
function printform() {
print <<
required
required
FORMEND;
makePageButtons();
}
?>
M/F
YYYY-MM-DD
(annual salary)
supervisor (by SSN)
address
department number