drnzpd579935 2016-10-20 16:36
浏览 80
已采纳

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

I have a cart where all my products are generated inside of a foreach loop that stores them in sessions. And i want to post each product in my Table:orders. But i am getting some errors.

Notice: Array to string conversion in C:\xampp\htdocs\system\clientes\gallery\postOrder.php on line 46

Notice: Array to string conversion in C:\xampp\htdocs\system\clientes\gallery\postOrder.php on line 54

what i am trying to post...

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
)

This is my foreach loop code

I am trying to post this way...

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();
                     }

What i am doing wrong,how i will fix this?

  • 写回答

2条回答 默认 最新

  • douzi9211 2016-10-20 16:51
    关注

    Your are trying to insert array instead of single item as value.

    Put all your insertion code inside a foreach loop

    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条)

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算