我正在尝试通过foreach
循环获取购物车数组。我可以成功获取数组项,当我echo
时,它会显示正确的值。但是当我对我的数据库表中输入的每个项目应用insert
查询时,它不会插入任何内容,但它会显示成功。我有4个名为:
primary
,姓名,电子邮件,密码,地址,电话,城市primary
,名称,描述,图片,价格primary
,日期,customerid foreign key
代码
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";
}
}
答案 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,...)