在PHP中,复选框数据保存在db中为“on”,Mysqli

时间:2017-07-18 14:12:39

标签: javascript php arrays mysqli php-7

错误: - 在db中保存的复选框数据为“on” - 使用explode,implode,in_array在获取复选框db数据进行更新时出现问题.----- 数据检索功能显示动态复选框以及反复数据以再次更新

HTML ----

<th>Qualification</th>
            <td> <div style="height:50; overflow:scroll">
            <?php echo get_checkbox_list("qualification","qualification_id","qualification_name","st_qul[]",$data[$col_id]);?>
            </div></td>

    function get_checkbox_list($table,$col_id,$col_value,$name,$sel=0)
    {
        global $con;
         $query= "SELECT * from $table";
         $rs=mysqli_query($con,$query) or die(mysqli_error("error"));
         $optionlist="";
         $sel=explode(",", $sel);

         while ($data=mysqli_fetch_assoc($rs))
          {


            if (in_array($data[$col_id], $sel)) {

                    $optionlist.="<input type=checkbox name=$name id=$data[$col_id] checked> $data[$col_value] <br>";
                }
                else{
                                    $optionlist.="<input type=checkbox name=$name id=$data[$col_id]> $data[$col_value] <br>";  
                }
         }
         return $optionlist;
    }

PHP:保存并更新mysqli查询-----

function save_student()
{
    global $con;
    $_REQUEST['st_qul']=implode(",",$_REQUEST['st_qul']);
    $img_name=$_FILES[st_photo][name];
    if ($img_name) 
    {
        $img_location=$_FILES[st_photo][tmp_name];
        $img_arr=explode(".",$img_name );
        $img_name=$img_arr[0]."_".time().".".$img_arr[1];
        move_uploaded_file($img_location, "../uploads/$img_name");
    }
    else
    {
    $img_name=$_REQUEST[st_photo];
    }
if ($_REQUEST['st_id'])
{
    $query="UPDATE `student_add` SET `st_name` = '".$_REQUEST['st_name']."',
    `st_father` = '".$_REQUEST['st_father']."', `st_add1` = '".$_REQUEST['st_add1']."',
    `st_add2` = '".$_REQUEST['st_add2']."', `st_city` = '".$_REQUEST['st_city']."',
    `st_state` = '".$_REQUEST['st_state']."',`st_country` = '".$_REQUEST['st_country']."',
                `st_nat` = '".$_REQUEST['st_nat']."', `st_gender` = '".$_REQUEST['st_gender']."',
                `st_qul` = '".$_REQUEST['st_qul']."', `st_course` = '".$_REQUEST['st_course']."',
                `st_photo` = '$img_name',`st_hobbies` = '".$_REQUEST['st_hobbies']."',
                `st_mobile` = '".$_REQUEST['st_mobile']."'
                WHERE `st_id` = $_REQUEST[st_id];";

                $msg="Data Updated Successfully..";
}
else{
        $query="INSERT INTO `student_add` (`st_name`, `st_father`, `st_add1`, `st_add2`, `st_city`, `st_state`, `st_country`, `st_nat`, `st_gender`, `st_qul`, `st_course`, `st_photo`, `st_hobbies`, `st_mobile`) VALUES ('".$_REQUEST['st_name']."', '".$_REQUEST['st_father']."', '".$_REQUEST['st_add1']."', '".$_REQUEST['st_add2']."', '".$_REQUEST['st_city']."', '".$_REQUEST['st_state']."', '".$_REQUEST['st_country']."', '".$_REQUEST['st_nat']."', '".$_REQUEST['st_gender']."', '".$_REQUEST['st_qul']."', '".$_REQUEST['st_course']."', '$img_name', '".$_REQUEST['st_hobbies']."', '".$_REQUEST['st_mobile']."');";

        $msg="Data Saved Successfully..";
}
    $rs=mysqli_query($con,$query);
    if ($rs)
     {
        header("Location:../student_view.php?msg=$msg");
     }
}

stored checkbox value

1 个答案:

答案 0 :(得分:0)

设置&#34;值&#34; -attribue

<input type="checkbox" name"foo" value="1"> // or whatever Value you like 

在POST上,&#34; foo&#34;如果检查了foo,将提供&#34; 1&#34;