The last part of this series involves the creation of a processing page. That is, a page that handles changes to the database. Below is the code I've written to a new php file called update.php. In the last part, there were 2 links: one to edit a contact record, and the other to delete a contact record. Both of these links pointed to this update.php file.
To begin, we need to access the database once again, like so:
<?php
// Connect to the database.
$host="localhost"; // Host name.
$db_user="root"; // MySQL username.
$db_password="root"; // MySQL password.
$database="mestalil"; // Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);
The next section of code handles each of the possible scenarios:
// If the URL includes an "action" parameter and an "id" parameter.
if (isset($_GET['action']) && isset($_GET['id'])) {
if ($_GET['action'] == "edit"){
// Select the data record with the selected id.
$sql = "select * from contacts where contact_id = '{$_GET['id']}'";
$qContacts = mysql_query($sql) or die(mysql_error());
// Load the retrieved record data into page variables.
while($row = mysql_fetch_array($qContacts)) {
$id = $row['contact_id'];
$firstname = $row['first_name'];
$lastname = $row['last_name'];
$phone = $row['phone'];
$email = $row['email'];
}
} elseif ($_GET['action'] == "del") {
// Delete the data record with the selected id.
$sql = "delete from contacts where contact_id = '{$_GET['id']}'";
mysql_query($sql) or die(mysql_error());
// Return to list of contacts page.
echo "
<script type=\"text/javascript\">
window.location=\"index.php\";
</script>
";
}
} elseif (isset($_POST['submit']) && isset($_POST['id'])) { // If this page was called from a Submit button variable.
// Reset posted variables into page variables.
$id = $_POST['id'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$email = $_POST['email'];
// Update SQL statement
$sql = "update contacts set first_name = '$firstname', last_name = '$lastname', phone = '$phone', email = '$email' WHERE contact_id = '$id'";
mysql_query($sql) or die(mysql_error());
// Return to list of contacts page.
echo "
<script type=\"text/javascript\">
window.location=\"index.php\";
</script>
";
}
?>
And finally, the PHP/HTML that is actually viewed by the user:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Maya's Contact CMS</title>
</head>
<body>
<form name="UpdateContact" action="update.php" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<table border="1" cellpadding="3" cellspacing="0">
<tr><th align="left">First Name:</th><td><input type="text" name="firstname" value="<?php echo $firstname; ?>" /></td></tr>
<tr><th align="left">Last Name:</th><td><input type="text" name="lastname" value="<?php echo $lastname; ?>" /></td></tr>
<tr><th align="left">Phone:</th><td><input type="text" name="phone" value="<?php echo $phone; ?>" /></td></tr>
<tr><th align="left">E-mail:</th><td><input type="text" name="email" value="<?php echo $email; ?>" /></td></tr>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Update" /></td></tr>
</table>
</form>
</body>
</html>
Of course, I don't think that it's the greatest code ever written, but it helped me to understand how to bind a MySQL application to my PHP code to create something useful.
If I get the chance, I want to create an "Add New Contact" page, but I think that would be almost identical to the way I've handled editing an existing contact. The major difference would be the SQL statement would look something more like this:
INSERT INTO contacts (
first_name,
last_name,
phone, email
) VALUES (
'$firstname',
'$lastname',
'$phone',
'$email'
);