在数据库中发布一个php foreach循环,数据插入错误

时间:2016-10-20 16:36:22

标签: php arrays foreach

我有一个购物车,我的所有产品都是在foreach循环中生成的,它将它们存储在会话中。 我想在我的表中发布每个产品:订单。 但是我遇到了一些错误。

注意:第46行的C:\ xampp \ htdocs \ system \ clientes \ gallery \ postOrder.php中的数组到字符串转换

注意:第54行的C:\ xampp \ htdocs \ system \ clientes \ gallery \ postOrder.php中的数组到字符串转换

我想发布的内容......

Array
(
    [numero] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
        )

    [vari] => Array
        (
            [0] => 
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [desenho] => Array
        (
            [0] => img/1.0.png
            [1] => img/1-1.png
            [2] => img/1-2.png
            [3] => img/1-3.png
        )

    [fabric] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 2
            [3] => 2
        )

    [size] => Array
        (
            [0] => 45
            [1] => 45
            [2] => 45
            [3] => 50
        )

    [qnty] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
        )

    //some other arrays 

    [submit_post] => Enviar
)

这是我的foreach loop code

我想以这种方式发帖......

if(isset($_POST['submit_post'])){
        $date = date('Y-m-d');
        $size = isset($_POST['size']) ? $_POST['size'] : array();
        $numero = isset($_POST['numero']) ? $_POST['numero'] : array();
        $vari = isset($_POST['vari']) ? $_POST['vari'] : array();
        $desenho = isset($_POST['desenho']) ? $_POST['desenho'] : array();
        $fabric = isset($_POST['fabric']) ? $_POST['fabric'] : array();
        $size = isset($_POST['size']) ? $_POST['size'] : array();
        $qnty = isset($_POST['qnty']) ? $_POST['qnty'] : array();
        $cost = isset($_POST['cost']) ? $_POST['cost']: array();
        $subtotal = isset($_POST['subtotal']) ? $_POST['subtotal'] : array();
        $total = isset($_POST['total']) ? $_POST['total'] : array();
        $all_products = isset($_POST['all_products']) ? $_POST['all_products'] : array();
        $subT=0;

        $pedido=$date." ".$_SESSION['userName']."-".$_SESSION['userLName'];
//46    $query = "SELECT * FROM almofadas WHERE id_price='$fabric'";
         $result = mysqli_query($conn,$query);
         while($rows = mysqli_fetch_assoc($result)){
            $tecido=$rows['tecido'];

        }

        $ins_sql = "INSERT INTO orders (fabric,size,product_quantity,order_id,product_img,product_title,variante,product_cost,product_subtotal) 
//54                    VALUES ('$fabric', '$size' , '$qnty', '$pedido', '$desenho', '$numero', '$vari', '$cost', '$subT')";
        if ($conn->query($ins_sql) === TRUE) {
            echo "New record created successfully";
            } else {
            echo "Error: " ;
            }
        $conn->close();
                     }

我做错了什么,我将如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您正在尝试插入数组而不是单个项目作为值。

将所有插入代码放在foreach循环

foreach($fabrics as $fabric)
{
    $pedido=$date." ".$_SESSION['userName']."-".$_SESSION['userLName'];
    $query = "SELECT * FROM almofadas WHERE id_price='$fabric'";
     $result = mysqli_query($conn,$query);
     while($rows = mysqli_fetch_assoc($result)){
        $tecido=$rows['tecido'];

    }

    // search for index of current item
    $index = array_search($f, $fabric);

    // write your insert code here where column values should be like $fabric[$index], $size[$index] and so on. 
    // I mean you need to fetch array element by index.
}

答案 1 :(得分:0)

修复变量,因为它们的默认值是数组:

  $fabric = isset($_POST['fabric']) ? $_POST['fabric'] : array();

执行:

  $fabric = isset($_POST['fabric']) ? $_POST['fabric'] : '';

相反。并且仅在设置$ fabric时执行SELECT查询。