简体   繁体   English

使用 foreach 循环将值插入数据库

[英]Insert values into database by using foreach loop

I get values in array for one parameter which is width.我在数组中获取一个参数的值,即宽度。

HTML file HTML 文件

<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div> 
<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div> 
<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div>  

After I submit values I get only one record in db for width, other variables are all the same except for width which is different.在我提交值后,我在 db 中只获得了一条宽度记录,除了宽度不同之外,其他变量都是相同的。

this is php code这是 php 代码

 if (isset($_POST['submit'])) {

    $width  = $_POST['width']; 
    $quantity =   $_POST['quantity'];
    $length =  $_POST['length']; 
    $productID =  $_POST['productID'];

    $date = date("Y-m-d H:i");

    foreach ($width as $value) { 

        $stmt=$conn->prepare("INSERT INTO testDB (width, quantity, length, productID)                 
        VALUES (:witdh, :quantity, :length, :productID) "); 

        $stmt->bindParam(':witdh',$value);
        $stmt->bindParam(':quantity',$quantity);
        $stmt->bindParam(':length',$length);
        $stmt->bindParam(':productID',$productID); 

        if($stmt->execute()){
            echo "success";
            exit();
        } 
        else{
            print_r($stmt->errorInfo()); 
        }  
    }   
}

When you do当你这样做

    if($stmt->execute()){
        echo "success";
        exit();
    } 

This will execute the prepared statement and if it succeeds (even in the first time around the loop) it will exit the code!这将执行准备好的语句,如果它成功(即使是在循环的第一次),它将退出代码!

You should also prepare the statement before the loop, so use...您还应该在循环之前准备语句,所以使用...

$stmt=$conn->prepare("INSERT INTO testDB (witdh, quantity, length, productID)                 
    VALUES (:witdh, :quantity, :length, :productID) "); 
$stmt->bindParam(':witdh',$value);
$stmt->bindParam(':quantity',$quantity);
$stmt->bindParam(':length',$length);
$stmt->bindParam(':productID',$productID); 
foreach ($width as $value) { 

    if($stmt->execute()){
        echo "success";
    } 
    else{
        print_r($stmt->errorInfo()); 
    }  
}   

You can set a flag to say if anything fails, how you handle this is up to you.您可以设置一个标志来说明是否有任何失败,您如何处理这取决于您。

You have to remove space from name input element.您必须从名称输入元素中删除空格。

<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div> 
<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div> 
<div class="col-sm-2"><input type="number" name="width[]" class="form-control"></div>  

If you check, I removed space from the name after width.如果您检查,我从名称中删除了宽度后的空格。

If will give proper output in print_r($_POST['width']);如果将在print_r($_POST['width']);

EDIT: Please remove exit();编辑:请删除exit(); as it is stopping the loop after the first insert.因为它在第一次插入后停止循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM