SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database Driven Sites - Rev3 - Chapter6

    Hi,
    I am writing the code for editauthors.php and had a few syntax problems that I have weeded out and now the page displays after click 'edit' sending the query

    http://localhost/mysql_database/editauthor.php?id10

    The interface is a little unintuitive as you have no idea what user you are editing but I am sure I can fix that with an echo $name $email above the form.

    The problem is that once I enter new information in to the edit form. It reports a success and I go back to the authors page to find the edit has not been made? I double checked via the terminal SELECT * FROM authors; and the read out matches the web page. So I am stuck, and I get no errors!?


    Code:
    <?php
    
    // A new author has been entered
    // Using the form below
    
    
    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', 'password');
    if (!$dbcnx) {
        exit('<p>Unable to connect to' . 
            'Datadabe server at this time</p>');
      }else{
        echo '<p>You have sucessfully conneted to the database server</p>';
    }
    
    
    // Select the jokes database
    if (!@mysql_select_db('ijdb')) {
      exit('<p>Unable to locate the joke ' .
          'database at this time</p>');
    }else{
      echo '<p>You have sucessfully conneted to the jokes database</p>';
    }
    
    if (isset($_POST['name'])):
      // The author's details have been updated.
    
      $name = $_POST['name'];
      $email = $_POST['email'];
      $id = $_POST['id'];
      $sql = "UPDATE author SET
              name='$name',
              email='$email'
              WHERE id='$id'";
      if (@mysql_query($sql)) {
        echo '<p>Author details updated.</p>';
      } else {
        echo '<p>Error updating author details: ' .
            mysql_error() . '</p>';
      }
    
    ?>
    
    <p><a href="authors.php">Return to authors list</a></p>
    
    <?php
    else: // Allow the user to edit the author
    
      $id = $_GET['id'];
      $author = @mysql_query(
          "SELECT name, email FROM author WHERE id='$id'");
      if (!$author) {
        exit('<p>Error fetching author details: ' .
            mysql_error() . '</p>');
      }
    
      $author = mysql_fetch_array($author);
    
      $name = $author['name'];
      $email = $author['email'];
    
      // Convert special characters for safe use
      // as HTML attributes.
      $name = htmlspecialchars($name);
      $email = htmlspecialchars($email);
    
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Edit the author:</p>
    <label>Name: <input type="text" name="name" value="<?php echo $name; ?>" /></label><br />
    <label>Email: <input type="text" name="email" value="<?php echo $email; ?>" /></label><br />
    <input type="hidden" name="id" value="<?php echo $id; ?>" />
    <input type="submit" value="SUBMIT" /></p>
    </form>
    
    <?php endif; ?>
    Any help would be grand!
    www.communiq-8.co.uk
    Computer repair, Graphic design and Web design

  2. #2
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You know what!

    Sometimes it is better to sit back and write on a forum to make you think...

    I entered this line above the form and notice there was output on screen for the variables $name and $email!?:

    Code:
    <?php echo '<p>You are editing the details of: ' . $name . $email . '</p>'; ?>

    Weird I thought, then looking at the url string and noticing it should actually be id=10.

    http://localhost/mysql_database/editauthor.php?id10


    So there was nothing wrong with the file at all!
    It was the paged handing over to editauthor.php that was the problem!
    HTML Code:
    <a href='editauthor.php?id$id'>Edit</a>
    Amended:
    HTML Code:
    <a href='editauthor.php?id=$id'>Edit</a>
    Sill don't know why there wasn't any error reporting but I will look at that.

    If there was any one piece of advice I could give anybody learning this stuff it would be, every time you have an instance of !$var exit echo 'unsuccessful' have an else echo 'report the successful command'.

    They can always be deleted once you know it works!

    From
    A. Noob
    www.communiq-8.co.uk
    Computer repair, Graphic design and Web design

  3. #3
    SitePoint Zealot atetlaw's Avatar
    Join Date
    Feb 2007
    Location
    Melbourne, Australia
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks for posting your solution. And the helpful advice

  4. #4
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, if I don't get a reply and find the solution, I find it's always best to add it.

    Makes the post a bit more worthwhile.

    I'll be back, trust me - Chapter 7 is Regular Expression euugh! - Dull as hell but probably because I don't get it.
    www.communiq-8.co.uk
    Computer repair, Graphic design and Web design


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
  •