Address Book Delete a Contact
- Address Book Sort Contacts By Name
- Address Book Sort Contacts By Type
- Address Book Add New Contact
- Address Book Edit a Contact
- Address Book Delete a Contact
- Address Book Security
This page is a tutorial on an app page that you use to delete a contact in your address book. Just one—not the whole db table.
On to the PHP codes: After getting ready to access the MySQL database by using the include with config.php in it, we use the $_POST function to get the value of the form input named answer into the PHP variable $A. If we find a "1" we stay with the PHP, otherwise the flow drops down to the HTML form. The "1" in $A means the form was submitted, and since the form specifically states that "This will delete this contact.", this means the user really does want to dump the record. What record? That comes next. But in such situations as deleting records, you always want an "out." The out here is a link that says "Return to Address Book—do NOT delete anything!" It goes to the home page.
The record id is now sought. The $_GET and $_POST functions are used to see if the id of the record being edited is available as it's supposed to be at this point. If it's not detected by either function, the user is sent to the home page. The reason we use both functions is the id value will have gotten sent by URL query string from either the Address Book Sort Contacts By Name or Address Book Sort Contacts By Type page, OR it will have gotten POSTed to the page from a hidden field in the form lower down in the page (i.e., the page sends itself data). How does this form know the correct id to send? A JavaScript section after the form uses JSON (JavaScript Object Notation) to get the PHP value $id and turn it into the JavaScript value id, then we stick the id into the form's hidden field using dot notation. Why do we need the form to POST this id value to the page? Because once the form is submitted, it submits it to this deleting page and this page reload will throw away all known values except those POSTed to the page during the reload. If we do not know the id, how will we know what record to delete? POSTing cures this ignorance.
Then the script uses the MySQL SELECT statement to get the record with that id. The mysql_num_rows() function is used to find out how many rows were in the result, and if the result is "none," the message "No such id." is displayed, and the user is sent to the home page. Next we use the MySQL statement "DELETE FROM addressbook WHERE id = '$id'" in a query to dump the record. We use the PHP function mysql_affected_rows() to see if 0 or 1 row was affected by this query. We see "The contact person deleting was successfully accomplished." if all went as it should, or "Deleting failed." if not. In either case, we're sent back to the home page which will show one less record, most likely.
On to the code for the script. Name the following:
delete-address-book-contact.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Delete Addressbook Contact</TITLE>
<meta name="description" content="Delete Addressbook Contact">
<meta name="keywords" content="Delete Addressbook Contact,Addressbook Contact,Addressbook,Content Management System,Content Management System Articles,php,CMS,javascript, dhtml, DHTML">
</head>
<body bgcolor="green">
<?php
include_once"../config.php";
$A=$_POST['answer'];if($A=="1"){
$id=$_GET['id']; if(!isset($id)){$id=$_POST['id'];if(!isset($id)){echo '<script language="javascript">alert("Start from Home page."); window.location = "address-book-sort-by-name.php"; </script>';}}
if (isset($id)){$check_id = mysql_query("SELECT * FROM addressbook WHERE id='$id'") or die(mysql_error());$m=mysql_num_rows($check_id);}
if ($m==0 || !isset($id) || !isset($m)){unset($id);echo '<script language="javascript">alert("No such id.");window.location = "address-book-sort-by-name.php"; </script>';}
$sql="DELETE FROM addressbook WHERE id = '$id'";
$result=mysql_query($sql) or die('Error ,deleting failed');
$rc = mysql_affected_rows();
if ($rc>0){echo '<script language="javascript">alert("The contact person deleting was successfully accomplished.");window.location ="address-book-sort-by-name.php"; </script>';}
else{echo '<script language="javascript">alert("Deleting failed.");window.location = "address-book-sort-by-name.php"; </script>';}
mysql_close();
}else{
?>
<div style='position:absolute; left:300px; top:200px;'>
<form id="form1" name="form1" method="post" action="delete-address-book-contact.php">
<table style='background-color:#eee' width="400" border="1" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><input type='hidden' name='id' value=''><input type="hidden" name="answer" value="1"></td>
</tr>
<tr>
<td>This will delete this contact.</td>
</tr>
<tr>
<td align=center><input type="submit" name="Submit" value="Delete This Contact"></td>
</tr>
<tr>
<td><a href="address-book-sort-by-name.php"><B>Return to Address Book—do NOT delete anything!</B></a></td>
</tr>
</table>
</form>
</div>
<script type="text/javascript">
var id = <?php echo json_encode($id); ?>;
document.form1.id.value=id;
</script>
<?php
}
?>
</body>
</html>