peppericious Posted December 2, 2012 Share Posted December 2, 2012 I have a page which shows me submissions made through a webpage. As I deal with those submissions, i.e. as I answer them, I want to update an 'answered' column to 1 (so that those flagged records won't be retrieved for display on the page any more). My retrieval query is like this: $q = "SELECT id, name, email, submission, submission_type, DATE_FORMAT(date_left, '%a, %D %b %Y @ %l:%i%p') date_left FROM submissions WHERE answered = 0 ORDER BY id DESC"; $r = mysqli_query($dbc, $q); The form displaying the found set has these lines: ... <input type='checkbox' name='answered[]' id='answered[]' /></p> <input type='hidden' name='id[]' id='id[]' value='$id' /> ... ... and the code I hoped would run the update is this: if(isset($_POST['submit'])) { $size = count($_POST['id']); $i=0; while($i<$size){ $id = $_POST['id'][$i]; if(isset($_POST['answered'][$i])) { $answered = 1; } else { $answered = 0; } $q = "UPDATE submissions SET answered = '$answered' WHERE id = '$id'"; $r = mysqli_query($dbc, $q); $i++; } It's not working, however. If I check, let's say 3 different records from the found set, the answered flag for the 3 most recent records' is being updated. Can anyone help? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/271498-flagging-certain-records-in-a-found-set/ Share on other sites More sharing options...
DavidAM Posted December 2, 2012 Share Posted December 2, 2012 Get rid of the hidden field, you don't need it. Put to record's ID in the name of the checkbox: echo "<input type='checkbox' name='answered[$id]' /></p>" Then process the answered array: if (isset($_POST['answered'])) { foreach ($_POST['answered'] as $id => $value) { UPDATE ... WHERE id = $id } } Note: Checkboxes are only POSTed if they are actually checked. So, if none are checked, the "answered" element of the POST array will not exist. Quote Link to comment https://forums.phpfreaks.com/topic/271498-flagging-certain-records-in-a-found-set/#findComment-1397006 Share on other sites More sharing options...
peppericious Posted December 3, 2012 Author Share Posted December 3, 2012 Excellent, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/271498-flagging-certain-records-in-a-found-set/#findComment-1397111 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.