dombrorj Posted November 22, 2011 Share Posted November 22, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/251629-cant-update-table-row/ Share on other sites More sharing options...
ManiacDan Posted November 22, 2011 Share Posted November 22, 2011 Both your inputs have the same name. Quote Link to comment https://forums.phpfreaks.com/topic/251629-cant-update-table-row/#findComment-1290484 Share on other sites More sharing options...
dombrorj Posted November 22, 2011 Author Share Posted November 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251629-cant-update-table-row/#findComment-1290497 Share on other sites More sharing options...
dombrorj Posted November 22, 2011 Author Share Posted November 22, 2011 Actually... It's working perfectly fine when updating the last row of the table results. But if I try to update any other row it changes that last row to a blank value and does nothing to the row I want to update. Getting closer! Quote Link to comment https://forums.phpfreaks.com/topic/251629-cant-update-table-row/#findComment-1290503 Share on other sites More sharing options...
ManiacDan Posted November 22, 2011 Share Posted November 22, 2011 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/251629-cant-update-table-row/#findComment-1290506 Share on other sites More sharing options...
dombrorj Posted November 22, 2011 Author Share Posted November 22, 2011 That did it, thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/251629-cant-update-table-row/#findComment-1290509 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.