Background: I took the CMS example from chapter 6 in "Build your Own Database Driven Website Using PHP&MySQL" and have attempted to modify it for use in my own database. It almost works for me LOL.
contact.php returns my dive buddies first and last name and gives me the option to either Edit or Delete them. Currently the delete option is not active.
When I choose to edit the contact, the last name and home email address fields populate with the correct information. (Once I figure out my mistake I'll be adding all the other fields that I have setup for this table so they can be edited).
I then make a change in one of the fields and click submit, both entries are deleted.
Below is the code from both contact.php and editdiver.php
I'm certain it is just a variable issue, my naming convention isn't the greatest. Sorry about that.
contact.php
Expand|Select|Wrap|Line Numbers
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <html>
- <head>
- <title>Joke CMS: Manage Authors</title>
- <meta http-equiv="content-type"
- content="text/html; charset=iso-8859-1" />
- </head>
- <body>
- <h1>Manage Dive Buddy's</h1>
- <ul>
Expand|Select|Wrap|Line Numbers
- <?php
- $dbcnx = @mysql_connect('localhost', 'root', 'password');
- if (!$dbcnx) {
- exit('<p>Unable to connect to the ' .
- 'database server at this time.</p>');
- }
- if (!@mysql_select_db('scubalog2')) {
- exit('<p>Unable to locate the joke ' .
- 'database at this time.</p>');
- }
- $diver2 = @mysql_query('SELECT id, first_name, last_name FROM contact');
- if (!$diver2) {
- exit('<p>Error retrieving authors from database!<br />'.
- 'Error: ' . mysql_error() . '</p>');
- }
- while ($diver = mysql_fetch_array($diver2)) {
- $id = $diver['id'];
- $name = htmlspecialchars($diver['first_name']);
- $last_name = htmlspecialchars($diver['last_name']);
- echo "<li>$name $last_name ".
- "<a href='editdiver.php?id=$id'>Edit</a> ".
- "<a href='deleteauthor.php?id=$id'>Delete</a></li>";
- }
- ?>
- </ul>
- <p><a href="newauthor.php">Add new author</a></p>
- <p><a href="index.html">Return to front page</a></p>
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Joke CMS: Edit Author</title>
- <meta http-equiv="content-type"
- content="text/html; charset=iso-8859-1" />
- </head>
- <body>
- <?php
- $dbcnx = @mysql_connect('localhost', 'root', 'password');
- if (!$dbcnx) {
- exit('<p>Unable to connect to the ' .
- 'database server at this time.</p>');
- }
- if (!@mysql_select_db('scubalog2')) {
- exit('<p>Unable to locate the diver ' .
- 'database at this time.</p>');
- }
- if (isset($_POST['name'])):
- // The author's details have been updated.
- $last_name = $_POST['last_name'];
- $email = $_POST['homeemail'];
- $id = $_POST['id'];
- $sql = "UPDATE contact SET
- last_name='$last_name',
- homeemail='$email'
- WHERE id='$id'";
- if (@mysql_query($sql)) {
- echo '<p>Diver details updated.</p>';
- } else {
- echo '<p>Error updating diver details: ' .
- mysql_error() . '</p>';
- }
- ?>
- <p><a href="contact.php">Return to dive buddy list</a></p>
- <?php
- else: // Allow the user to edit the author
- $id = $_GET['id'];
- $diver2 = @mysql_query(
- "SELECT last_name, homeemail FROM contact WHERE id='$id'");
- if (!$diver2) {
- exit('<p>Error fetching diver details: ' .
- mysql_error() . '</p>');
- }
- $diver = mysql_fetch_array($diver2);
- $last_name = $diver['last_name'];
- $homeemail = $diver['homeemail'];
- // Convert special characters for safe use
- // as HTML attributes.
- $last_name = htmlspecialchars($last_name);
- $homeemail = htmlspecialchars($homeemail);
- ?>
- <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
- <p>Edit the author:</p>
- <label>Name: <input type="text" name="name" value="<?php echo $last_name; ?>" /></label><br />
- <label>Email: <input type="text" name="email" value="<?php echo $homeemail; ?>" /></label><br />
- <input type="hidden" name="id" value="<?php echo $id; ?>" />
- <input type="submit" value="SUBMIT" /></p>
- </form>
- <?php endif; ?>
- </body>
- </html>