Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am attempting to develop a PHP script to record presence at an event for those persons in a mysql table Persons.  I am attempting to use checkboxes to indicate who was present.  Here is the script as currently written:


<?php # Script
require ('includes/mysqli_connect.php');
if(isset($_POST['checkbox'])) {$checkbox = $_POST['checkbox'];
if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"]);
$id = "('" . implode( "','", $checkbox ) . "');" ;
echo $id;
$q="UPDATE Persons SET present = '".(isset($activate)?'Y':'N')."' WHERE id IN $id" ;
$r = mysqli_query($dbc,$q) or die(mysqli_error());
$q = "SELECT * FROM Persons";
$r = @mysqli_query ($dbc,$q);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=?http="//www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset="utf-8"" />
<title>Update multiple rows in mysql with checkbox</title>
<script type="text/javascript">
function un_check(){
for (var i = 0; i < document.frmactive.elements.length; i++) {
var e = document.frmactive.elements;
if ((e.name != 'allbox') && (e.type == 'checkbox')) {
e.checked = document.frmactive.allbox.checked;
<table width="400" border="0" cellspacing="1" cellpadding="0">
<td><form name="frmactive" method="post" action="checkboxes.php">
<table width="400" border="0" cellpadding="3" cellspacing="1">
<td colspan="5"><input name="activate" type="submit" id="activate" value="Activate" />
<input name="deactivate" type="submit" id="deactivate" value="Deactivate" /></td>
<td> </td>
<td colspan="4"><strong>Update multiple rows in mysql with checkbox</strong> </td>
<td align="center"><input type="checkbox" name="allbox" title="Select or Deselect ALL" style="background-color:#ccc;"/></td>
<td align="center"><strong>id</strong></td>
<td align="center"><strong>first_name</strong></td>
<td align="center"><strong>last_name</strong></td>
<td align="center"><strong>present</strong></td>
while ($row = mysqli_fetch_array($r)) {
  <td align="center"><input name="checkbox[]" type="checkbox" id="checkbox" id="checkbox[]" value="<? echo $rows['person_id']; ?>"></td>
  <td><? echo $row['person_id']; ?></td>
  <td><? echo $row['first_name']; ?></td>
  <td><? echo $row['last_name']; ?></td>
  <td><? echo $row['present']; ?></td>
<td colspan="5" align="center"> </td>


The table Persons contains these columns: person_id, first_name,last_name,present.


I inserted the echo line after the line defining $id which is copied below:

$id = "('" . implode( "','", $checkbox ) . "');" ;
echo $id;
But $id only contains empty strings.
Can anyone help me understand what I am doing wrong?
Is $checkbox always ending up empty?
Any direction will be appreciated.
Wes Smith



Link to post
Share on other sites

The $_POST variable in PHP stores keys as per the name attribute of HTML input elements.

The name attribute of your input elements is checkbox[], not checkbox.


Add the following to the top of your code, and I think you'll quickly see the issue. Thanks.

echo '<pre>';
echo '</pre>';
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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