存储数组复选框值

时间:2012-10-18 17:25:46

标签: php

我有一个没有用户的表

$listuser = mysql_query('SELECT id,firstname,lastname,approve FROM user');
echo '<form action="" method="post">';
echo '<table>
          <tr><td>Name</td>
              <td>AdminApprove</td>
          </tr>';
while($row = mysql_fetch_assoc($listuser))
{
  $id = $row['id'];
  $firstname = $row['firstname'];
  $lastname  = $row['lastname'];
  echo '<tr>
           <td><input type="hidden" name="id[]" value ='.$id.'></td>
           <td>'.$firstname.''.$lastname.'</td>
            <td><input type="checkbox" name="approve[]"></td>
        </tr>';
}
echo '<tr><td><input type="submit" name="submit" value="submit"></td></tr>';
echo '</table></form>';

提交表格后

   $postedid = $_POST['id'];
   $approve = isset($_POST['approve']) ? 1 : 0;   
   $approvestudent = 'UPDATE user SET approve = '.$approve.' WHERE id = '.$postedid.'';
   $result_update = mysql_query($approvestudent);  

我想为每个学生一次批准一个或多个,并且批准的复选框值需要以0或1存储到数据库

1 个答案:

答案 0 :(得分:0)

$_POST['id']是数组不是字符串或整数

您可以使用php serialize()函数将数组转换为字符串

 $postedid = $_POST['id'];
   $approve  = $_POST['approve'];

应该是

 $postedid = serialize($_POST['id']);
   $approve  = serialize($_POST['approve']);