SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    429
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    UPDATE not working ...

    I have posted a simplified version of what I am trying to accomplish. The index page has two functions: to display the contents of the database table (each record having a link to the edit page along with its id#) and a form to insert new records into the database table. The second page has a form for updating the selected record. For some reason, this page is not picking up the form values to update the database with, although I am not getting any error messages. As a result, my update just wipes out the record.

    I have tried echoing the query (all values are blank) and echoing other values along the way, but I can't seem to pinpoint the source of the problem. Can anyone help me with this?

    Here is the index.php page:
    PHP Code:
    <?php
    require_once('inc/connect_db.php');

    $submit $_POST['submit'];

    // Add new record to database
    if ($submit == 'Add Record') {
            
        
    $fullname3 addslashes($_POST['fullname']);
        
    $age3 addslashes($_POST['age']);
        
    $date3 addslashes($_POST['date']);
        
        
    $query "INSERT INTO test_updates
        SET fullname = '
    $fullname3',
        age = '
    $age3',
        date = '
    $date3' ";
                
        
    $result mysql_query($query$mysql_link);
        
    $id3 mysql_insert_id($result);            
        
    }
    ?>

    <?php include_once('inc/header.php'); ?>
                        
    <div id = "wrapper" style="padding: 30px;">    
        <h1>Testing Update Database</h1>
    <div id="database">        
        <p>
            <form action="index.php" name="update_form" method="post" style="margin-left: 50px;">
            <input type="submit" name="submit" class="button" value="Display Records" />
            </form>
        </p>
         
        <?php 
            
    //display all information
        
    if ($submit == 'Display Records') {
        
    ?>
        
        <!--print out table containing the records-->            
        <table cellspacing="0">
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Age</td>
                <th>Date</th>
                <th>&nbsp;</th>
            </tr>
            
            <?php
            
    // select all the records
            
    $query "SELECT id, fullname, age, date
            FROM test_updates
            ORDER BY age ASC "
    ;
                
            
    $result mysql_query($query$mysql_link);
            
            while (
    $record mysql_fetch_array($result)) {
                
    $id $record['id'];
                
    $fullname stripslashes($record['fullname']);
                
    $age stripslashes($record['age']);
                
    $date stripslashes($record['date']);
                
                
    $record_timestamp strtotime($date);
                
    $day date('D, M j, Y'$record_timestamp);
            
    ?>    
            
                <tr>
                    <td style="text-align: center;"><?php print $id?></td>
                    <td style="text-align: left;"><?php print $fullname?></td>
                    <td style="text-align: center;"><?php print $age?></td>
                    <td style="text-align: center;"><?php print $date?></td>
                    <td><a href="edit_record.php?id=$id">Edit</a></td>
                </tr>
                
            <?php
            
    }
            
    ?>
            
            </table>
            
        <?php
        
    }
        
    ?>
    </div>

    <div id="form">
        <form action="index.php" method="post">
            <h3>Enter a Record</h3>
            <p>Name:    <input type="text" name="fullname" class="textbox" value="<?php print stripslashes($fullname3); ?>" /></p>
                    <input type="hidden" name="id" class="textbox" value="<?php print $id3?>"  />
            <p style="margin-left: 15px;">Age: <input type="text" name="age" class="textbox" value="<?php print stripslashes($name3); ?>" /></p>
            <p style="margin-left: 10px;">Date:  <input type="text" name="date" class="textbox" value="<?php print stripslashes($date3); ?>" /></p>
            <p style="margin-left: 60px;"><input type="submit" name="submit" class="button" value="Add Record" /></p>        
        </form>
    </div>    
    <div class="clear"></div>

    </div>  <!-- end of wrapper div -->

    </body>

    </html>
    and here is the edit_record.php page:

    PHP Code:
    <?php
    require_once('inc/connect_db.php');

    $id $_GET['id'];
    $submit $_POST['submit'];

    // test for id and submit variables
    print("<p style=\"font-weight: bold; color: green;\">$id :: $submit</p>");

    if (
    $submit == "Edit Record") {
        
        
    $fullname addslashes($_POST['$fullname']);
        
    $age $_POST['$age'];
        
    $date $_POST['$date'];
            
        print(
    "<p style=\"font-weight: bold; color: green;\">$id :: $fullname :: $age :: $date</p>");
        
        
    $update_query "UPDATE test_updates
        SET fullname = '
    $fullname',
        age = '
    $age',
        date = '
    $date'
        WHERE id = '
    $id' ";
        
        
    $update_result mysql_query($update_query$mysql_link);
        
        print(
    "$update_query");
        
    }

    $query "SELECT id, fullname, age, date
        FROM test_updates
        WHERE id = '
    $id' ";

        
    $update_result mysql_query($query$mysql_link);
        
    $row mysql_fetch_row($update_result);

        
    $id3 $row[0];
        
    $fullname3 stripslashes($row[1]);
        
    $age3 $row[2];
        
    $date3 $row[3];
        
        print(
    "<p style=\"font-weight: bold; color: green;\">$id :: $fullname :: $age :: $date</p>");
    ?>

    <?php include_once('inc/header.php'); ?>
                
    <div id = "wrapper2">
        <h1>Edit Records</h1>
        
        <div id="form2">
            <form action="edit_record.php?id=<?php print $id3?>" method="post">
                <h3>Enter a Record</h3>
                <p>Name:<input type="text" name="fullname" class="textbox" value="<?php print $fullname3?>" /></p>
                        <input type="hidden" name="id" class="textbox" value="<?php print $id3?>"  />
                <p style="margin-left: 15px;">Age: <input type="text" name="age" class="textbox" value="<?php print $age3?>" /></p>
                <p style="margin-left: 10px;">Date:  <input type="text" name="date" class="textbox" value="<?php print $date3?>" /></p>
                <p style="margin-left: 60px;"><input type="submit" name="submit" class="button" value="Edit Record" /></p>        
            </form>
        </div>    
        <div class="clear"></div>
        <a href="index.php" style="margin-left: 300px;">Return to Home Page.</a>        
    </div>  <!-- end of wrapper div -->

    </body>        

    </html>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    This line in index.php is not correct
    HTML Code:
    <td><a href="edit_record.php?id=$id">Edit</a></td>
    it should be
    HTML Code:
    <td><a href="edit_record.php?id=<?php echo $id; ?>">Edit</a></td>

  3. #3
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    429
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry ... that was a typo when I transferred the code to this forum. I had that originally, and the id# does get passed through to the edit_record.php page, and gets picked up when I update, but the other values of the record are not picked up. That is my main issue. Could you please check the edit_record.php code to see if I did something wrong there? Thanks.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    $fullname = addslashes($_POST['$fullname']);
    $age = $_POST['$age'];
    $date = $_POST['$date'];
    The form fields are called fullname, age and date. Not $fullname, $age and $date.

  5. #5
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    429
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Okay. Now I'm really embarassed ... I was doing a lot of cutting and pasting to save time, and missed that. Thanks for your help. It works now.


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
  •