Jump to content

Can't Update Table Row


dombrorj

Recommended Posts

I'm pretty inexperienced with coding and trying to get a row of a MySQL database to update when a form is submitted, but can't seem to get it to work.  :'(

 

Here's what I have...

 

if($_POST['action'] == 'update'){
    //update
    $rename = $_POST['rename'];
    $uid = $_POST['rowid'];
    mysql_query("UPDATE `master` SET `name` = '$rename' WHERE `id` = '$uid'");

}

 

Then the form...

<?php

        $result = mysql_query("SELECT * FROM `master`");
        if(mysql_num_rows($result)){
        while ($row = mysql_fetch_array($result)){
        ?>
          <td><?php echo $row['name']; ?></a></td>
            <td><input type="text" size="5px" name="rename" value="" /></td>
          
           <td><input type="hidden" name="rename" value="<?php echo $row['name']; ?>" /><input type="hidden" name="rowid" value="<?php echo $row['id']; ?>" /><input type="hidden" name="action" value="update" /><input type="submit" value="Update" /></td>
            </tr>
            <?php
			}
		}
        ?>

 

Can anyone give me some hints as to why this won't work? Thaks!

Link to comment
https://forums.phpfreaks.com/topic/251629-cant-update-table-row/
Share on other sites

Good catch! That helped a bit. Now it's doing 2 things, neither of which are what I'd like to do:

 

1. It's not updating the correct row id. If I change "hidden" to "text" for the rowid, it is displaying the correct row. But when I hit the submit button, it only updates the last item in the table, no matter which row I'm submitting

 

2.No matter what I put in the "rename" field, it updates as a blank field. I tried changing to

 <input type="text" size="5px" name="rename" value="<?php echo $row['name']; ?>" /></td>
but nothing happens. If I fille value="test" again it only updates the last row in the table to "test." Entering information into the form field does nothing.

You need one form for every row.  Right now, from the looks of it, your form contains X copies of each element, so only the LAST ones are being submitted. Put opening and closing form tags, as well as submit buttons, in each row inside the loop.

 

-OR-

 

Name your inputs like this:

?>
<input name="newRowContents[<?php echo $row['id']; ?>]" type="text"  value="<?php echo $row['name']; ?>" />
<input name="oldRowContents[<?php echo $row['id']; ?>]" type="hidden" value="<?php echo $row['name']; ?>" />
<?php

Now, $_POST will be an array of arrays, $_POST['newRowContents'] will be an array of the boxes you type in, keyed by row ID.  $_POST['oldRowContents'] will be an array of the old row contents, also keyed by row ID.  So you can:

foreach ( $_POST['oldRowContents'] as $rowID => $contents ) {
  if ( $contents != $_POST['newRowContents'][$rowID] && !empty($_POST['newRowContents'][$rowID]) ) {
    //do your update for this row
  }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.