发布多个复选框php mysql

时间:2014-04-16 16:10:17

标签: php mysql checkbox

我的表单数据我列出了我的表样本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

3 个答案:

答案 0 :(得分:1)

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

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

快速提示:您非常容易受到SQL注入的影响。如果这是生产代码,要么使用准备好的查询,要么使用像PDO这样的PHP数据库包装器来为您完成。不推荐使用mysql_...函数。

答案 1 :(得分: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')

答案 2 :(得分:1)

    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

        }
}?>