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 20.3, paste them into a new file called delentry.php, and change "View" to "Delete" in lines 24 and 38.

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

Listing 20.4. Script Called delentry.php for Selecting and Deleting a Record

43: } else if ($_POST) { 44:     //check for required fields 45:     if ($_POST["sel_id"] == "")  { 46:         header("Location: delentry.php"); 47:         exit; 48:     } 49: 50:    //issue queries 51:    $del_master_sql = "DELETE FROM master_name WHERE 52:                       id = '".$_POST["sel_id"]."'"; 53:    $del_master_res = mysqli_query($mysqli, $del_master_sql) 54:                      or die(mysqli_error($mysqli)); 55: 56:    $del_address_sql = "DELETE FROM address WHERE 57:                        id = '".$_POST["sel_id"]."'"; 58:    $del_address_res = mysqli_query($mysqli, $del_address_sql) 59:                       or die(mysqli_error($mysqli)); 60: 61:    $del_tel_sql = "DELETE FROM telephone WHERE id = '".$_POST["sel_id"]."'"; 62:    $del_tel_res = mysqli_query($mysqli, $del_tel_sql) 63:                   or die(mysqli_error($mysqli)); 65: 66:    $del_fax_sql = "DELETE FROM fax WHERE id = '".$_POST["sel_id"]."'"; 67:    $del_fax_res = mysqli_query($mysqli, $del_fax_sql) 68;                   or die(mysqli_error($mysqli)); 69: 70:    $del_email_sql = "DELETE FROM email WHERE id = '".$_POST["sel_id"]."'"; 71:    $del_email_res = mysqli_query($mysqli, $del_email_sql) 72:                     or die(mysqli_error($mysqli)); 73: 74:    $del_note_sql = "DELETE FROM personal_notes WHERE 75:                     id = '".$_POST["sel_id"]."'"; 76:    $del_note_res = mysqli_query($mysqli, $del_note_sql) 77:                    or die(mysqli_error($mysqli)); 78: 79:    $display_block = "<h1>Record(s) Deleted</h1> 80:    <p>Would you like to 81:    <a href=\"".$_SERVER["PHP_SELF"]."\">delete another</a>?</p>"; 82: } 83: ?> 84: <html> 85: <head> 86: <title>My Records</title> 87: </head> 88: <body> 89: <?php echo $display_block; ?> 90: </body> 91: </html>

Picking up with line 45, the script looks for the required field, $_POST["sel_id"], just as it did in the selentry.php script. If that required value does not exist, the user is redirected to the selection form. In lines 5177, queries delete all information related to the selected individual, from all tables. Lines 7981 place a nice message in $display_block, and the script exits and prints the HTML to the screen. Figure 20.6 shows an output of the record deletion script.

Figure 20.6. Deleting a record.


When you go back to the record selection form after deleting a record, you'll note that the individual you deleted is no longer in the selection menuas it should be!




Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net