如何使用我的代码检索选中的复选框?

时间:2014-03-09 07:41:39

标签: php mysql post

HTML CODE:

<input type='checkbox' name='cbox[]' value='Jaywalking'  /> 
Jaywalking<br>
<input type='checkbox' name='cbox[]' value='Littering'/> 
Littering<br>
<input type='checkbox' name='cbox[]' value='Illegal Vendor' /> 
Illegal Vendor

POST CODE:

if(is_array($_POST['cbox'])) $violation_save=implode(',',$_POST['cbox']); else $violation_save=$_POST['cbox'];

mysql_query("UPDATE tblcitizen SET violation='$violation_save' WHERE id='$id'") or die mysql_error());

将检查后的复选框值检索到数据库中的最简单方法是什么?根据我的代码检索它的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

试试这段代码,

<form method='post'>
<input type='checkbox' name='cbox[]' value='Jaywalking'  /> 
Jaywalking<br>
<input type='checkbox' name='cbox[]' value='Littering'/> 
Littering<br>
<input type='checkbox' name='cbox[]' value='Illegal Vendor' /> 
Illegal Vendor
    <input type='submit' name='submit' />
</form>
<?php
if(isset($_POST['submit'])){
    $violation_save = '';
    if(is_array($_POST['cbox']))
        $violation_save=implode(',',$_POST['cbox']);
    else
        $violation_save=$_POST['cbox'];
    //debug purpose
    var_dump($violation_save);
    $sql = "UPDATE `tblcitizen` SET `violation`='$violation_save' WHERE `id`='$id'";
    var_dump($sql);
    mysql_query($sql) or die(mysql_error());
}
?>

这只是展示了存储在$_POST['cbox']中的值,它使用var_dump显示为输出