【问题标题】:post from multiple checkboxes php mysql从多个复选框 php mysql 发布
【发布时间】:2014-04-16 16:10:17
【问题描述】:

我的表单数据列出了我的表格示例 10 条记录。

<td class='table-checkbox'><input type="checkbox" name="product[]" value="1" class='selectable-checkbox'></td>
    <td><div class="input-mini">
    <input type="text" id="spinnn"  name="quantity[]" value="5" class='spinner'/>
    </div>
    </td>
    </tr>

..... .....

我的 php 代码

$carino = $_POST['carino']; 
$quantity = $_POST['quantity']; 
$product = $_POST['product'];
foreach($product as $product){ 
foreach($quantity as $quantity ){ 
$sql = "INSERT INTO mytable (product,quantity,cno) VALUES ('$product','$quantity','$carino')";
mysql_query($sql);
}}

我想将此数据插入我的表中。但是我的 foreach 错了怎么办?

产品独一无二

my table

product - quantity - cno
1            5        2   
2            10       2

http://pastie.org/private/nwrrinnxlrumt6p3kuyq#11,13-14,19

【问题讨论】:

    标签: php mysql checkbox


    【解决方案1】:

    这个:

    <?php 
    
    if(isset($_POST['submit'])) {
    
        $carino = "2"; 
    
        $adet = $_POST['adet'];
        $urunno = $_POST['urunno'];
        $total = count($_POST['adet']);
    
        echo '<hr>'; 
    
        foreach ($urunno as $checked)
                {
                $value = $checked - 1;
                    echo "Value of Urunno: $checked:  Adet: $adet[$value] <br>";
                    echo "INSERT INTO member_interests
                        (`urun`,`adet`,'carino')
                    VALUES
                        ('$checked','$adet[$value]','$carino')<br>";
                }
        }
    ?>
    
    <tr>
    <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <td class='table-checkbox'><input type="checkbox" name="urunno[]" value="1">Product One</td>
    <input type="text" id="spinnn"  name="adet[]" class='spinner'/>
    <td class='table-checkbox'><input type="checkbox" name="urunno[]" value="2">Product Two</td>
    <input type="text" id="spinnn"  name="adet[]" class='spinner'/>
    <td class='table-checkbox'><input type="checkbox" name="urunno[]" value="3">Product Three</td>
    <input type="text" id="spinnn"  name="adet[]" class='spinner'/>
    <td><div class="input-mini">
    <input type="submit" name="submit" value="run" />
    </form>
    </div>
    </td>
    </tr>
    </tbody>
    </table>
    </div>
    

    输出:

    Value of Urunno: 2: Adet: 2 
    INSERT INTO member_interests (`urun`,`adet`,'carino') VALUES ('2','2','2')
    Value of Urunno: 3: Adet: 3 
    INSERT INTO member_interests (`urun`,`adet`,'carino') VALUES ('3','3','2')
    

    【讨论】:

    • 错了,因为 urunno 必须是唯一的,我更新了我的问题。
    • 这是一个处理数据的例子,你已经嵌套了 foreach 循环,我希望你能直观地看到发生了什么。我为你把它改成了一个 for 循环,向你展示结果会是什么。
    • 你好,我发现了一个错误。例如,我列出了两种产品。我选择了两个产品没问题。但我首先选择产品没有问题。但我只选择第二个产品我的 adet 值是 0 但 urun id 正确如何解决这个错误?
    • 如果我选择所有复选框不会出错。否则我检查两个或一个复选框代码没有运行,问题出在哪里?
    • 因为你必须传递一个值,即使它是 0
    【解决方案2】:

    逻辑可以如下(自己想代码,或者网上搜索):

    • 确保所有 POST 数组的长度相同(应该是)
    • 从 0 到数组长度的计数循环
    • 将每个数组中该点的值插入到数据库中。

    快速提示:您很容易受到 SQL 注入的影响。如果这是生产代码,请使用准备好的查询,或者使用像 PDO 这样的 PHP 数据库包装器来为您完成。 mysql_... 函数已弃用。

    【讨论】:

      【解决方案3】:
          Try this one  
            <?php
              //add the database connection
              ?>
              <form name="myform" action="">
              <table>
              <?php 
              $length=//how many time below check box repeat in this page
              for($i=0;$i<$length;$i++){?>
              <tr>
              <td class='table-checkbox'>
              <input type="checkbox" name="urunno[]" value="<?php echo $i;?>" class='selectable-checkbox'>
              </td>
              <td>c1k</td>
              <td>2147483647</td>
              <td>520</td>
              <td>435345345 A.Ş.</td>
              <td>
              <div class="input-mini">
              <input type="text" id="spinnn"  name="adet<?php echo $i;?>" class='spinner'/>
              </div>
              </td>
              </tr>
              <?php
              }?>
              </table>
              <input type="submit" name="submit" value="Submit">
              </form>
              <?php
              if(isset($_post['submit']))
              {
              $carino = $_POST['carino'];  //here this element not present in question, i am also follow this
              $adet = $_POST['adet']; 
              $urunno = $_POST['urunno'];
              $length=sizeof($urunno);
              for($i=0;$i<$length;$i++)
              {
                $urun1=$urunno[$i];
                $adet1=$adet[$urun1];
                $sql = "INSERT INTO table (urunno,adet,cno) VALUES ('$urun1','$adet1','$carino')";
                mysql_query($sql);
      
                //the above code insert sql work in every time //otherwise use batch insert
                 //refrer this link http://stackoverflow.com/questions/12960569/mysql-bulk-insert-via-php
      
              }
      }?>
      

      【讨论】:

      • 如果我选择所有复选框不会出错。否则我检查两个或一个复选框代码没有运行,问题出在哪里?
      • 现在请检查一下吗?
      猜你喜欢
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多