Jump to content

Flagging Certain Records In A Found Set


peppericious

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.