setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //prepare the sql statement which returns how many employees match this employee id $selected_employee = "SELECT COUNT(*) FROM Employee WHERE Emp_No='$employeeid'"; //prepare the select statement to be executed $no_of_employees = $conn->prepare($selected_employee); //prepare the sql statement to delete a record $delete_sql = "DELETE FROM Employee WHERE emp_no='$employeeid'"; /*prepare the select statement to return the number of projects (if any which the specific selected employee has allocated against their name */ $no_of_projects = "SELECT COUNT(*) FROM Emp_On_Project WHERE Emp_No='$employeeid'"; //execute the no_of_projects query if ($res2 = $conn->query($no_of_projects)) { /* Check the number of rows that match the SELECT statement */ if ($res2->fetchColumn() > 0) { //if the number of projects allocated against the selected employee is more than zero //then issue a warning - i.e. you must delete the projects first before deleting the //employees! - the foreign key constraint will stop you executing the DELETE query! echo "Warning!

"; echo "There are project allocations for Emp_No $employeeid."; echo "You must delete the employee allocations first before deleting the employee!

"; echo "

Display All Employees    "; echo "Insert New Employee

"; } //otherwise you can prepare to delete the chosen employee from the employee table of the database else { //executed the query to find out how many employees match this employee id if ($res = $conn->query($selected_employee)) { /* Check the number of rows that match the SELECT statement */ if ($res->fetchColumn() > 0) { //if more than one row is returned then execute the delete query // use exec() because no results are returned $count = $conn->exec($delete_sql); /* Delete statement executed */ print("You executed the following SQL statement: $delete_sql
"); /* Return number of rows that were deleted */ print("

Deleted $count rows from the employee table.

"); } /* No rows matched -- do something else */ else { /* Return the message below */ print "No rows matched the query."; } } //display the links at the bottom of the page to the other two php pages echo "

Display All Employees    "; echo "Insert New Employee

"; } } } //deal with any PDO connection execeptions. catch(PDOException $e) { echo $selected_employee . "
" . $e->getMessage(); } $res = null; //close the database connection $conn = null; ?>