插入查询回显成功但不在mysql数据库表

时间:2015-12-07 19:31:31

标签: php mysql database

我正在尝试通过foreach循环获取购物车数组。我可以成功获取数组项,当我echo时,它会显示正确的值。但是当我对我的数据库表中输入的每个项目应用insert查询时,它不会插入任何内容,但它会显示成功。我有4个名为:

的表
  1. 客户 - >序列primary,姓名,电子邮件,密码,地址,电话,城市
  2. 产品 - > productid primary,名称,描述,图片,价格
  3. 订单 - >序列primary,日期,customerid foreign key
  4. order_detail - > orderid,productid,数量,价格,颜色,尺寸
  5. 当我点击按钮时,应该更新order_detail和orders表。 这是我的代码。任何建议将不胜感激。

    代码

    if (isset($_SESSION['login_email'])) {
        $email = $_SESSION['login_email'];
        //Insert values in order table. This works correctly!!
        $query = mysqli_query($con, "SELECT serial FROM customers WHERE email = '$email'");
        while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
            $cust_id = $row['serial'];
            $date = date('Y-m-d');
            $que = mysqli_query($con, "INSERT INTO orders VALUES('', '$date', '$cust_id')");
        }
    
        // Fetching serial from orders table. This also works correct.
        $q = mysqli_query($con, "SELECT serial FROM orders WHERE customerid = '$cust_id'");
        while ($row1 = mysqli_fetch_array($q, MYSQLI_ASSOC)) {
            $serial = $row1['serial'];
            echo $serial ."\n";
        }
        //Problem comes here. Here i am trying to insert values in order_details
        foreach ($_SESSION['cart'] as $id => $value) {
            $subtotal = $value['price'] * $value['quantity'];
            $pid = $value['id'];
            $quantity = $value['quantity'];
            $color = $value['color'];
            $size = $value['size'];
        }
        mysqli_query($con, "INSERT INTO order_detail VALUES ($serial, $pid, $quantity, $subtotal, $color, $size)");
        if (true) {
            echo "success";
        } else {
            echo"not success";
        }
    }
    

1 个答案:

答案 0 :(得分:0)

你有没试过这个?

if( mysqli_query($con, "INSERT INTO order_detail VALUES ($serial, $pid, $quantity, $subtotal, $color, $size)") ){
   echo "success";
    } else {
        echo"not success";
    }

我认为你最好能以这种方式进行插入:

INSERT INTO order_detail (column1, column2, column3,...)
VALUES (value1, value2, value3,...)