Eccentricson Posted October 24, 2013 Share Posted October 24, 2013 (edited) I have a form, it's reading the mysql database. I have a while loop to display all of the fields in the table so you can edit it. Yes, it displays the fields, but I'm having problems with the update part, when I specify the ID (which is a hidden field) it takes the last field in the form and uses that to update. For example: UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6';UPDATE mitigations SET `status`='Watch', `notes`='test6' WHERE id='6'; I need id to actually get the ID from the form where it's a hidden field. It works now, but only for the last field. Make sense? Here's the actual code I have on the edit form itself: <?php while ($data = mysql_fetch_array($mitigation_query)) {echo "<form><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=". $data['id'] ."><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=". $data['status'] ."></span></td><td width='758'><input type=text name='mitigation_id' value=". $data['id'] ."><textarea rows=5 cols=100 name='mitigation_notes'>" . $data['notes'] ."</textarea></td></tr></form>"; } ?> Edited October 24, 2013 by Eccentricson Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/ Share on other sites More sharing options...
.josh Posted October 24, 2013 Share Posted October 24, 2013 when you rightclick > view source, do you see correct ids or are they all outputting the same (last) id? where is your submit button in relation to this? What code actually submits the form? My *guess* is that since you don't have a submit button in each of the forms, you must have a single submit button and js doing something to submit. So the problem is likely somewhere in the js Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455255 Share on other sites More sharing options...
Eccentricson Posted October 24, 2013 Author Share Posted October 24, 2013 when you rightclick > view source, do you see correct ids or are they all outputting the same (last) id? where is your submit button in relation to this? What code actually submits the form? My *guess* is that since you don't have a submit button in each of the forms, you must have a single submit button and js doing something to submit. So the problem is likely somewhere in the js Yes, the IDs are correct for each row it spits out. The submit button is in the same form tag. I have a form in the beginning, then this form at the while loop and I close both at the end of the file. I don't have any JS associated either. Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455261 Share on other sites More sharing options...
Ch0cu3r Posted October 24, 2013 Share Posted October 24, 2013 (edited) Are you wanting to update multiple records at the same time? You need to setup you form like this echo '<form action="edit_page.php" method="post"> <table> <tr> <th>Status</th> <th>Notes</th> </tr>'; while ($data = mysql_fetch_array($mitigation_query)) { $row_id = $data['id']; echo " <tr> <td width='56' height='20px'> <span id='MitigationOnTrack'><input type='text' name='mitigation[$row_id][status]' value=". $data['status'] ."></span> </td> <td width='758'> <input type=text name='mitigation[$row_id][id]' value=". $data['id'] ."> <textarea rows=5 cols=100 name='mitigation[$row_id][notes]'>" . $data['notes'] ."</textarea> </td> </tr>"; } echo ' <tr> <td colspan="2"><input type="submit" name="update" value="Update" /></td> <!-- one update field --> </tr> </table> </form>'; Then to update the records you'd use if(isset($_POST['update'])) { // loop through the form entries foreach($_POST['mitigation'] as $row_id => $mitigation) { $status = mysql_real_escape_string($mitigation['status']); $notes = mysql_real_escape_string($mitigation['notes']); $row_id = (int) $row_id; // update the row mysql_query("UPDATE mitigations SET `status`='$status', `notes`='$notes' WHERE id='$row_id'"); } } Edited October 24, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455262 Share on other sites More sharing options...
.josh Posted October 24, 2013 Share Posted October 24, 2013 okay well if the correct ids are being output then i don't think this is a php problem. can you please clarify or post the view-source? because at a minimum you are definitely doing something wrong with those form tags wrapped around each set of elements. Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455265 Share on other sites More sharing options...
Eccentricson Posted October 24, 2013 Author Share Posted October 24, 2013 Here's the sourcecode: <tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=1><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=1><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=2><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=2><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=3><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=3><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=4><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=4><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=5><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=5><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr><tr><td width='56' height='20px'><input type=hidden name='mitigation_id' value=6><span id='MitigationOnTrack'><input type='text' name='mitigation_status' value=Watch></span></td><td width='758'><input type=text name='mitigation_id' value=6><textarea rows=5 cols=100 name='mitigation_notes'></textarea></td></tr> Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455268 Share on other sites More sharing options...
.josh Posted October 24, 2013 Share Posted October 24, 2013 i could have gleaned that from just outputting the loop you provided..i meant your "full" (relevant) view-source.. you know, the other parts like, that mythical form submit button you speak of. Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455270 Share on other sites More sharing options...
.josh Posted October 24, 2013 Share Posted October 24, 2013 basically what i'm getting at here is that your form is improperly structured. You wrap your grouped elements in form tags that point to nowhere and have no submit buttons within them. So how does one actually submit the changes? And if you aren't using javascript to grab that stuff.. well already I know one problem is that your submit button (assuming it exists - i have yet to see it) can't possibly be associated with that stuff. But I can't tell you how to fix it unless I see the full picture. But in general, the solution is going to look something along the lines of what Ch0cu3r showed. For starters, notice how he only outputs a single form tag wrapped around the entire thing. Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455271 Share on other sites More sharing options...
Eccentricson Posted October 24, 2013 Author Share Posted October 24, 2013 basically what i'm getting at here is that your form is improperly structured. You wrap your grouped elements in form tags that point to nowhere and have no submit buttons within them. So how does one actually submit the changes? And if you aren't using javascript to grab that stuff.. well already I know one problem is that your submit button (assuming it exists - i have yet to see it) can't possibly be associated with that stuff. But I can't tell you how to fix it unless I see the full picture. But in general, the solution is going to look something along the lines of what Ch0cu3r showed. For starters, notice how he only outputs a single form tag wrapped around the entire thing. Unfortunately, due to the sensitive data, I can't post the source code, without significant data scrubbing. I have changed it have one single form, I have figured the best way to fix this, is to use variables for the names of the fields. Simply because the last ID in the database was setting the data for that name and the form was using the same data over and over until the while loop ended. If this makes sense. Your guys help has helped me a lot, I really appreciate it!! If I was making any money on this project, I'd donate. Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455303 Share on other sites More sharing options...
Eccentricson Posted October 25, 2013 Author Share Posted October 25, 2013 Are you wanting to update multiple records at the same time? You need to setup you form like this echo '<form action="edit_page.php" method="post"> <table> <tr> <th>Status</th> <th>Notes</th> </tr>'; while ($data = mysql_fetch_array($mitigation_query)) { $row_id = $data['id']; echo " <tr> <td width='56' height='20px'> <span id='MitigationOnTrack'><input type='text' name='mitigation[$row_id][status]' value=". $data['status'] ."></span> </td> <td width='758'> <input type=text name='mitigation[$row_id][id]' value=". $data['id'] ."> <textarea rows=5 cols=100 name='mitigation[$row_id][notes]'>" . $data['notes'] ."</textarea> </td> </tr>"; } echo ' <tr> <td colspan="2"><input type="submit" name="update" value="Update" /></td> <!-- one update field --> </tr> </table> </form>'; Then to update the records you'd use if(isset($_POST['update'])) { // loop through the form entries foreach($_POST['mitigation'] as $row_id => $mitigation) { $status = mysql_real_escape_string($mitigation['status']); $notes = mysql_real_escape_string($mitigation['notes']); $row_id = (int) $row_id; // update the row mysql_query("UPDATE mitigations SET `status`='$status', `notes`='$notes' WHERE id='$row_id'"); } } This worked with a few tweaks to it. Thanks so much!!! I appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/283242-php-form-submissions-for-while-loops/#findComment-1455334 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.