dongtiao2066 2017-07-18 14:12 采纳率: 0%
浏览 27

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

Error :- Checkbox data saved in db as "on" - used explode, implode, in_array so problem in fetching checkbox db data for updating.----- Data retrieve function to show dynamic checkbox as well as retrive data to again update

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: Save and update mysqli query-----

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条回答 默认 最新

  • dpppic5186 2017-07-18 14:30
    关注

    Set the "Value"-attribue

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

    On POST, "foo" will supply "1", if foo is checked.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法