By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,855 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,855 IT Pros & Developers. It's quick & easy.

Unable to UPDATE record in database

P: 8
Hi folks I think I've got a variable problem but not 100% sure.

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
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <html>
  5. <head>
  6. <title>Joke CMS: Manage Authors</title>
  7. <meta http-equiv="content-type"
  8.     content="text/html; charset=iso-8859-1" />
  9. </head>
  10. <body>
  11. <h1>Manage Dive Buddy's</h1>
  12. <ul>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $dbcnx = @mysql_connect('localhost', 'root', 'password');
  4. if (!$dbcnx) {
  5.   exit('<p>Unable to connect to the ' .
  6.       'database server at this time.</p>');
  7. }
  8.  
  9. if (!@mysql_select_db('scubalog2')) {
  10.   exit('<p>Unable to locate the joke ' .
  11.       'database at this time.</p>');
  12. }
  13.  
  14. $diver2 = @mysql_query('SELECT id, first_name, last_name FROM contact');
  15. if (!$diver2) {
  16.   exit('<p>Error retrieving authors from database!<br />'.
  17.       'Error: ' . mysql_error() . '</p>');
  18. }
  19.  
  20. while ($diver = mysql_fetch_array($diver2)) {
  21.   $id = $diver['id'];
  22.   $name = htmlspecialchars($diver['first_name']);
  23.   $last_name = htmlspecialchars($diver['last_name']);
  24.   echo "<li>$name $last_name ".
  25.       "<a href='editdiver.php?id=$id'>Edit</a> ".
  26.       "<a href='deleteauthor.php?id=$id'>Delete</a></li>";
  27. }
  28.  
  29. ?>
  30. </ul>
  31. <p><a href="newauthor.php">Add new author</a></p>
  32. <p><a href="index.html">Return to front page</a></p>
  33. </body>
  34. </html>
  35.  
editdiver.php
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Joke CMS: Edit Author</title>
  6. <meta http-equiv="content-type"
  7.     content="text/html; charset=iso-8859-1" />
  8. </head>
  9. <body>
  10. <?php
  11.  
  12. $dbcnx = @mysql_connect('localhost', 'root', 'password');
  13. if (!$dbcnx) {
  14.   exit('<p>Unable to connect to the ' .
  15.       'database server at this time.</p>');
  16. }
  17.  
  18. if (!@mysql_select_db('scubalog2')) {
  19.   exit('<p>Unable to locate the diver ' .
  20.       'database at this time.</p>');
  21. }
  22.  
  23. if (isset($_POST['name'])):
  24.   // The author's details have been updated.
  25.  
  26.   $last_name = $_POST['last_name'];
  27.   $email = $_POST['homeemail'];
  28.   $id = $_POST['id'];
  29.   $sql = "UPDATE contact SET
  30.           last_name='$last_name',
  31.           homeemail='$email'
  32.           WHERE id='$id'";
  33.   if (@mysql_query($sql)) {
  34.     echo '<p>Diver details updated.</p>';
  35.   } else {
  36.     echo '<p>Error updating diver details: ' .
  37.         mysql_error() . '</p>';
  38.   }
  39.  
  40. ?>
  41.  
  42. <p><a href="contact.php">Return to dive buddy list</a></p>
  43.  
  44. <?php
  45. else: // Allow the user to edit the author
  46.  
  47.   $id = $_GET['id'];
  48.   $diver2 = @mysql_query(
  49.       "SELECT last_name, homeemail FROM contact WHERE id='$id'");
  50.   if (!$diver2) {
  51.     exit('<p>Error fetching diver details: ' .
  52.         mysql_error() . '</p>');
  53.   }
  54.  
  55.   $diver = mysql_fetch_array($diver2);
  56.  
  57.   $last_name = $diver['last_name'];
  58.   $homeemail = $diver['homeemail'];
  59.  
  60.   // Convert special characters for safe use
  61.   // as HTML attributes.
  62.   $last_name = htmlspecialchars($last_name);
  63.   $homeemail = htmlspecialchars($homeemail);
  64.  
  65. ?>
  66.  
  67. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  68. <p>Edit the author:</p>
  69. <label>Name: <input type="text" name="name" value="<?php echo $last_name; ?>" /></label><br />
  70. <label>Email: <input type="text" name="email" value="<?php echo $homeemail; ?>" /></label><br />
  71. <input type="hidden" name="id" value="<?php echo $id; ?>" />
  72. <input type="submit" value="SUBMIT" /></p>
  73. </form>
  74.  
  75. <?php endif; ?>
  76.  
  77. </body>
  78. </html>
  79.  
Oct 15 '07 #1
Share this Question
Share on Google+
1 Reply


MarkoKlacar
Expert 100+
P: 296
Hi Markw,

not quite sure what could be wrong, the only thing I spotted was on line 25,26 of the contact.php file.
[PHP]
echo "<li>$name $last_name "."<a href='editdiver.php?id=$id'>Edit</a> "."<a href='deleteauthor.php?id=$id'>Delete</a></li>";[/PHP]

Should be:
[PHP]
echo "<li>$name $last_name "."<a href='editdiver.php?id='$id'>Edit</a> "."<a href='deleteauthor.php?id='$id'>Delete</a></li>";[/PHP]

Don't know if this solves the problem but at least it's start, at least I hope so.

Cheers
Oct 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.