SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unable to UPDATE record in database

    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
    PHP Code:
    <!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 Divers</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <h1>Manage Dive Buddy's</h1>
    <ul>
    <?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>
    editdiver.php
    PHP Code:
    <!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 Diver</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>

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,329
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    First thing I noticed is;
    In editdiver.php, your assignment $last_name = $_POST['last_name']; will always return a null because in the ELSE clause your form does not have a field with the name "last_name".
    Also, you are checking for the presence of "name" in the $_POST, but your SQL UPDATE uses the $id for the Where. So, what if you get a POST that has no ID?

    These are quick observations without taking too much time to examine the code.

    I hope this helps.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ParkinT,

    I'll look at your first half the post some.

    As for the id it will always be present, it is an auto assigned primary key for the contact table.
    Mark

  4. #4
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Parkin1,

    Thanks for the suggestions, changed $last_name = $_POST['last_name']; to $last_name = $_POST['name']; amd everything started to work correctly.

    Thanks again.

    Mark


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •