如何从复选框中插入多个值并将其插入mysql中

时间:2017-05-25 13:46:45

标签: php mysql

<tr>
<td><label style="padding-top:10px"> flower </label></td>
<td><input type="checkbox" name="pol[]" value="lili"/> lili
<input type="checkbox" name="pol[]" value="tulip"/> tulip
<input type="checkbox" name="pol[]" value="rose"/> rose</td>
</tr>
<input class="btn btn-success" name="submit" value="Submit" type="submit">

我有这样的输入和像这样的PHP代码

        $id = $_POST['id'];
        $pol  = implode(",",$_POST['pol']);

        $query ="INSERT INTO data_rs (id_rs) values ('$id')";
        $eksekusi=mysql_query($query);

        if(isset($_POST['pol']))
        {
            include ('inc/konek.php');
            $sql ="INSERT INTO rs_fl(id_rs,id_fl) VALUES ((SELECT id_rs FROM 
 data_rs WHERE id_rs ='$id'),
            (SELECT id_fl FROM poliklinik WHERE Flower  ='$pol'))";
            $ek = mysql_query($sql);
        }
        else
        {
            echo "<script> alert ('please insert data !'); window.history.back()</script>";
    }

我有3个表&#39; data_rs&#39;,&#39; poliklinik&#39;&#39; rs_fl&#39;每个包含

    data_rs                poliklinik                       rs_fl    
id_rs    name            id_fl      Flower           id(AI)  id_rs  id_fl
r01       type41          f01        lili           
                          f02        tulip 
                          f03        rose

当我从复选框中检查列表多个值时我想要的结果是我的表rs_fl就像这个值id_rs具有不同的值id_fl

  data_rs                poliklinik                       rs_fl    
id_rs    name            id_fl      Flower            id(AI)  id_rs id_fl
r01       type41          f01        lili             1     r01     f01
                          f02        tulip            2     r01     f02
                          f03        rose             3     r01     f03

但我不知道何时选中该复选框,如果poliklinikrs_fl只有1个值可以显示,但是当我从复选框表rs_fl中选择多个值时,则为空。因为我的英语不好而且还在使用mysql这是旧的program.thnx

1 个答案:

答案 0 :(得分:0)

    $id = $_POST['id'];
    $pol  = implode(",",$_POST['pol']);

如果已设置,则在测试之前使用$ _POST ['pol'] $pol变量可以包含一个或多个花。

    $query ="INSERT INTO data_rs (id_rs) values ('$id')";
    $eksekusi=mysql_query($query);

    if(isset($_POST['pol']))
    {
        include ('inc/konek.php');
            $sql ="INSERT INTO rs_fl(id_rs,id_fl) VALUES ((SELECT id_rs FROM 
 data_rs WHERE id_rs ='$id'),

您正在选择Flower = '$pol',但是如果$ pol拥有多个花,您正在搜索类似Flower ='lili,rose'的内容。这个名字没有花

    (SELECT id_fl FROM poliklinik WHERE Flower  ='$pol'))";
    $ek = mysql_query($sql);
}
else
{
    echo "<script> alert ('please insert data !'); window.history.back()</script>";

}

可以做很多事情来改进你的代码,但你应该做的最少的事情是测试是否设置了$ _POST ['pol'],以及它是否有多个值。如果是,您应该使用WHERE Flower in ( $pol values go here)

        $id = $_POST['id'];
        $multiple = false;
        $pol = '';

        if (isset($_POST['pol'])) {
          $pol  =  implode('", "', $_POST['pol']);
          if (count($_POST['pol']) > 1)
             $multiple = true;
        }
        $query ="INSERT INTO data_rs (id_rs) values ('$id')";
        $eksekusi=mysql_query($query);

        if ( ! empty($pol) )
        {
            include ('inc/konek.php');
            if ( $multiple )
              $flower = "='". $pol . "'";
            else
              $flower = 'IN ("' . $flower .'" )';

                $sql ="INSERT INTO rs_fl(id_rs,id_fl) VALUES ((SELECT id_rs FROM 
     data_rs WHERE id_rs ='$id'),
                (SELECT id_fl FROM poliklinik WHERE Flower $flower))";
                $ek = mysql_query($sql);
            }
            else
            {
                echo "<script> alert ('please insert data !'); window.history.back()</script>";
        }

未经测试,因此可能仍需要一些微调