472,982 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

Unable to UPDATE record in database

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
1 2372
MarkoKlacar
296 Expert 100+
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

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

Similar topics

2
by: Mark | last post by:
A beginner in this area, I have been able to read a record from a MySQL database and populate an HTML form (wow!). Now, my goal is to allow the user to edit the contents of the form and then...
2
by: fig000 | last post by:
Hi, I have an application that's running fine on development servers (web and database-sql server 2000). I'm updating a record through a third party component but I don't think the component...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
3
by: Mihaly | last post by:
I want to update a record into a table in SQL Server 2000 database from C#. This table is used concurently for other users too, and I want to be sure that from the read of record to the update no...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.