473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 2393
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.