将复选框值存储到mysql数据库

问题描述:

I have problem in storing checkbox values in mysql db (only the last value is stored in the database). How can I fix this code to store all checked values?

<label class="q" for="q1">Which, if any, of these activities did you do on the Internet in the last 30 days? 
</label><br><br>
<input name="q1[]" type="checkbox" value="a1">Used e-mail<br>
<input name="q1[]" type="checkbox" value="a2">Used instant messenger & chat room<br>
<input name="q1[]" type="checkbox" value="a3">Made a purchase for personal use<br>
<input name="q1[]" type="checkbox" value="a4">Downloaded/Played a video game<br>
<input name="q1[]" type="checkbox" value="a5">Obtained news/information/current events<br>
<input name="q1[]" type="checkbox" value="a6">Looked for employment (Used classified listings)<br>
<input name="q1[]" type="checkbox" value="a7">Looked for recipes<br>
<input name="q1[]" type="checkbox" value="a8">Downloaded a movie<br>
<br>



<?php

include('config.php');

$tbl_name="temp_members_db";
$q1=$_POST['q1'];
$sql="INSERT INTO $tbl_name(q1)VALUES('$q1')";
?>

You can use implode http://php.net/manual/en/function.implode.php

$q1=implode(',', $_POST['q1']);

        <html>
    <head><title>Example</title></head>
    <body>
        <form action="" method="post">
            <table width="50%">
            <tr>
        <td><input type="checkbox"name="boxes[]"value="8am">1</input></td>
                <td>8am</td>
            </tr>
            <tr>
        <td><inputtype="checkbox"name="boxes[]"value="9am">2</input></td>
                <td>9am</td>
            </tr>
            <tr>
        <td><input type="checkbox"name="boxes[]"value="10am">3</input></td>
                <td>10am</td>
            </tr>
            </table>
            <input type="submit" name="submit">
        </form>
        <?php
            if(isset($_POST['submit'])){
            require_once("config.php");
            if(isset($_POST['boxes'])){
            $t1=implode(',', $_POST['boxes']);
            $s = "insert into new(time) values('$t1')"; 
                $res=mysql_query($s);
                if($res){
                    echo "insert success";
                }else{
                    echo "error in inserting";
                }
          }

        }

       ?>
    </body>
</html>

please try this.

I experienced the same issue for check boxes implode() works like a charm. For radio buttons you'll need to use an associative array for implode() to work.

but I think create a new table for those Items refer to main id. then you can dynamically handle this: I need a checkbox about payment options like:

<div class="form-group row">
                                <label for="default-input" class="col-md-2 control-label">Payment Options</label>
                                <div class="col-md-10">
                                    <input name="pamentoptionsValue[]" id="checkbox-0" value="1" class="" type="checkbox">
                                    <label for="checkbox-0" class="control-label"> Cash </label>
                                    <input name="pamentoptionsValue[]" id="checkbox-1" value="2" class="" type="checkbox">
                                    <label for="checkbox-1" class="control-label"> Visa </label>
                                    <input name="pamentoptionsValue[]" id="checkbox-1" value="3" class="" type="checkbox">
                                    <label for="checkbox-1" class="control-label"> MasterCard </label>
                                    <input name="pamentoptionsValue[]" id="checkbox-1" value="4" class="" type="checkbox">
                                    <label for="checkbox-1" class="control-label"> American  </label>
                                    <input name="pamentoptionsValue[]" id="checkbox-1" value="5" class="" type="checkbox">
                                    <label for="checkbox-1" class="control-label"> Express</label>
                                    <input name="pamentoptionsValue[]" id="checkbox-1" value="6" class="" type="checkbox">
                                    <label for="checkbox-1" class="control-label"> Nexus</label>
                                   
                                </div>
                            </div>

this use for different company Id then I store it like this way:

 for($u=0;$u<count($_POST['pamentoptionsValue']);$u++){
                $pamentoptionsValue =$_POST['pamentoptionsValue'][$u];
                //$mdeceased =isset($_POST['mdeceased'])?$_POST['mdeceased']:null;
                if($pamentoptionsValue==1){
                    $pamentoptionsName='Cash';
                }elseif($pamentoptionsValue==2){
                    $pamentoptionsName='Visa';
                }elseif($pamentoptionsValue==3){
                    $pamentoptionsName='MasterCard';
                }elseif($pamentoptionsValue==4){
                    $pamentoptionsName='American';
                }elseif($pamentoptionsValue==5){
                    $pamentoptionsName='Express';
                }elseif($pamentoptionsValue==6){
                    $pamentoptionsName='Nexus';
                }elseif($pamentoptionsValue==7){
                    $pamentoptionsName='bKash';
                }elseif($pamentoptionsValue==8){
                    $pamentoptionsName='Payza';
                }
                if($pamentoptionsValue){
                    $query5 = "INSERT INTO pamentoptions (companyId,pamentoptionsDeleted,pamentoptionsCreatedBy,pamentoptionsValue,pamentoptionsName) VALUES ('$companyId','0','$companyCreatedBy','$pamentoptionsValue','$pamentoptionsName')";
                    $inserted_rows5 = $db->insert($query5);
                }
            }
</div>