在for循环中插入查询

时间:2017-01-23 05:39:00

标签: php mysqli

我目前正在处理一个生成字段的表单。

我的问题是当我尝试插入数据库时​​,它成功了,但是插入的唯一数据是最后一个字段。

我试图回应结果并且它工作正常。

我甚至尝试使用foreach,但结果相同。

我希望你能帮助我。

CODE

require_once 'inc.connection.php';

$shipping_code = generateRandomString();
$packaging = $_POST['packaging']; 
$quantity = $_POST['quantity'];
$total_weight = $_POST['total_weight'];
$length = $_POST['length'];
$width = $_POST['width'];
$height = $_POST['height'];
$unit = $_POST['unit'];
$freight_class = $_POST['freight_class'];



$counter_count = count($packaging);

for($a=0; $a < $counter_count; $a++)
{


    $sql = "INSERT INTO shipping_items (user_id, shipping_code, packaging, quantity, total_weight, length, width, height, unit, freight_class)
    VALUES ('$_SESSION[id]', '$shipping_code', '$packaging[$a]', '$quantity[$a]', '$total_weight[$a]', '$length[$a]', '$width[$a]', '$height[$a]', '$unit[$a]', '$freight_class[$a]')";


}

1 个答案:

答案 0 :(得分:1)

在循环中添加语句:mysql_query($ sql)...它将解决您的问题