Creating the Record Deletion Mechanism

The record deletion mechanism is virtually identical to the script used to view a record. In fact, you can just take the first 42 lines of Listing 19.3 and paste them into a new file, called delentry.php, and make the following changes:

  • In lines 7, 37, and 43, change "view" to "delete"

  • In lines 24 and 39, change "View" to "Delete"

Starting with a new line 45, the remainder of the code for delentry.php is shown in Listing 19.4.

Listing 19.4 Script Called delentry.php for Selecting and Deleting a Record
 45:    //check for required fields
 46:      if ($_POST[sel_id] == "") {
 47:        header("Location: delentry.php");
 48:        exit;
 49:    }
 50: 
 51:    //issue queries
 52:    $del_master = "delete from master_name where id = $_POST[sel_id]";
 53:    mysql_query($del_master);
 54: 
 55:    $del_address = "delete from address where id = $_POST[sel_id]";
 56:    mysql_query($del_address);
 57: 
 58:    $del_tel = "delete from telephone where id = $_POST[sel_id]";
 59:    mysql_query($del_tel);
 60: 
 61:    $del_fax = "delete from fax where id = $_POST[sel_id]";
 62:    mysql_query($del_fax);
 63: 
 64:    $del_email = "delete from email where id = $_POST[sel_id]";
 65:    mysql_query($del_email);
 66: 
 67:    $del_note = "delete from personal_notes where id = $_POST[sel_id]";
 68:    mysql_query($del_master);
 69: 
 70:    $display_block = "<h1>Record(s) Deleted</h1>
 71:    <P>Would you like to
 72:    <a href=\"$_SERVER[PHP_SELF]\">delete another</a>?</p>";
 73: }
 74: ?>
 75: <HTML>
 76: <HEAD>
 77: <TITLE>My Records</TITLE>
 78: </HEAD>
 79: <BODY>
 80: <? print $display_block; ?>
 81: </BODY>
 82: </HTML>

Picking up with Line 45, the script looks for the required field, $_POST[sel_id]. If that value does not exist, the user is redirected to the selection form. In lines 52?68, queries delete all information related to the selected individual, from all tables. Lines 70?72 place a nice message in $display_block, and the script exits and prints the HTML to the screen. An output of the record deletion script is shown in Figure 19.6.

Figure 19.6. Deleting a record.

graphics/19fig06.gif

Now go back to the record selection form and note that the individual you deleted is no longer in the selection menu.



    Part III: Getting Involved with the Code