【问题标题】:store checkbox value to mysql database [duplicate]将复选框值存储到mysql数据库[重复]
【发布时间】:2013-08-11 10:44:32
【问题描述】:

我在 mysql db 中存储复选框值时遇到问题(只有最后一个值存储在数据库中)。 如何修复此代码以存储所有检查的值?

<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')";
?>

【问题讨论】:

标签: php mysql checkbox


【解决方案1】:
【解决方案2】:
        <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>

请试试这个。

【讨论】:

【解决方案3】:

我遇到了同样的问题,复选框 implode() 就像一个魅力一样工作。 对于单选按钮,您需要使用关联数组才能使 implode() 工作。

【解决方案4】:

但我认为为这些项目创建一个新表是指主 ID。那么你可以动态处理这个: 我需要一个关于付款选项的复选框,例如:

<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>

这用于不同的公司 ID 然后我像这样存储它:

 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);
                }
            }

【讨论】:

  • 这个答案不仅忽略了 OP 的示例上下文,还展示了一些不良做法。 HTML 文档不应包含重复的元素 ID。痛苦的if-elseif 块应该替换为switch()match() 或查找数组。虽然在插入新记录时遵循规范化,但将用户输入直接写入 sql 字符串是不安全且不稳定的(请注意,如果用户输入未被 if 块捕获,则用户数据将直接馈送到查询——不好。
猜你喜欢
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
相关资源
最近更新 更多