从数据库中检索一个或多个复选框值

时间:2014-03-05 02:30:00

标签: php

这是我的HTML代码

   <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

这是我的PHP代码

if(isset($_POST['save']))
{   
    $license_save=$_POST['license'];
    $stickerno_save=$_POST['stickerno'];
    $fname_save=$_POST['fname'];
    $mname_save=$_POST['mname'];
    $lname_save=$_POST['lname'];
    $no_save=$_POST['no'];
    $street_save=$_POST['street'];
    $city_save=$_POST['city'];
    $bdate_save=$_POST['bdate'];


    $violationplace_save=$_POST['violationplace'];
    $dd_save=$_POST['dd'];
    $mm_save=$_POST['mm'];
    $yy_save=$_POST['yy'];
    $hh_save=$_POST['hh'];
    $min_save=$_POST['min'];
    $ampm_save=$_POST['ampm'];

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

mysql_query("UPDATE tblcitizen SET license ='$license_save', stickerno ='$stickerno_save', fname ='$fname_save', mname ='$mname_save', lname ='$lname_save', no ='$no_save', street ='$street_save', city ='$city_save', bdate ='$bdate_save',   violationplace ='$violationplace_save', dd ='$dd_save', mm ='$mm_save', yy ='$yy_save', hh ='$hh_save', min ='$min_save', ampm ='$ampm_save', violation ='$violation_save', type ='$type_save', officer ='$officer_save', date ='$date_save', ttime ='$ttime_save' WHERE id = '$id'")
                or die(mysql_error()); 
    echo "<script>alert('Successfully Edited')</script>";
        header("Location: citizen.php");

}

我想编辑一些已注册的帐户,如何从数据库中检索一个或多个复选框值。

2 个答案:

答案 0 :(得分:0)

为更好的答案而编辑

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

您的查询正在使用cbox数组,如果它是一个数组(如果选中了多个复选框),则将其变为带逗号的字符串,否则只插入一个没有逗号的复选框值。

要获取值,只需读取字符串,然后将其与php strpos()

进行比较
<?
    // Get the checkboxes that were previously selected
    $result = mysql_query("SELECT violation FROM tblcitizen WHERE id = '$id'") or die(mysql_error());
      while ($row = mysql_fetch_assoc($result)) {
         // put the string into a variable
         $mystring = $row['violation'];
      }

    // use a ternary to determine if this checkbox exists
    // if so, make the variable $checked to check the checkbox, otherwise leave it blank
    $checked = (strpos($mystring, 'Jaywalking')) ? 'checked' : '';
?>
    // write the checkbox to the page. and echo the contents of $checked
    // if it was found with strpos() then it will be checked, otherwise it will not be
    // <?= ?> is a quick way to echo a variable without the echo command
    <input type='checkbox' name='cbox[]' value='Jaywalking' <?=$checked;?> /> 

答案 1 :(得分:0)

我不太明白你的句子“如何从数据库中检索1个或多个复选框值”,但由于我可以看到你的更新语句,我假设你想要从中获取一个或多个值复选框并更新数据库中的表。

  1. 您将收到数组形式的复选框值
  2. 您需要遍历数组并获取值
  3. 将值存储在一个变量中,您最好将其存储在数据库中

    foreach ($cbox as $val) {
        $Violation .= $val.",";
    }
    
  4. 顺便说一句,您总是可以使用PHP子字符串删除最后一个逗号。干杯,希望这可以帮助你和那里的任何人。谢谢。

相关问题