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 关于模型训练的一个问题
  • ¥15 装了几千台服务器从来没遇到这种问题,哎看下哪位帮我解决吧
  • ¥15 单片机程序上的困难问题
  • ¥15 请教某软件缓存Ts文件破解合并mp4的方法
  • ¥15 求小游戏炸弹人中关于敌人的C++代码
  • ¥15 拿到这个服务器最高权限有偿
  • ¥50 来个抓app跳转支付宝转链接的
  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 以下代码,运行结果报错