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
https://forums.phpfreaks.com/topic/271498-flagging-certain-records-in-a-found-set/
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.

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.