[英]How to store my session array into my database
How do I store my data in session array into my database?如何将 session 数组中的数据存储到我的数据库中? Upon entering the product and its quantity, the user will be directed to a checkout page.输入产品及其数量后,用户将被引导至结帐页面。 In the checkout page, upon filling up the necessary information in a form, I am trying to use a button to submit the session array into my database.在结帐页面中,填写表格中的必要信息后,我尝试使用按钮将 session 数组提交到我的数据库中。
My current code:我当前的代码:
<?php
session_start();
$array = $_SESSION['shopping_cart'];
$connect = mysqli_connect('localhost', 'root', '', 'table');
?>
$sql = "INSERT INTO customer_order (productName, quantity, totalPrice, pax)";
foreach($array as $product){
$sql = "INSERT INTO p2_5.customer_order (productName, quantity, totalPrice, pax)";
$sql .= " VALUES ('{$product['name']}', '{$product['quantity']}', '{$product['price']}', '{$product['pax']}')";
if ($connect->query($sql)) {
$errorMsg = "Connection failed: " . $connect->connect_error;
$success = false;
}
}
However, upon clicking on the button, I am able to store client information into my database, but I would have an PHP error message Undefined index: name, Undefined index: quantity, Undefined index: price, Undefined index: pax?但是,单击按钮后,我可以将客户信息存储到我的数据库中,但我会收到 PHP 错误消息 Undefined index: name, Undefined index: quantity, Undefined index: price, Undefined index: pax?
You're passing elements from $array
without referencing to their indexes
, I mean without $array[0]
or $array[1]
.您从$array
传递元素而不引用它们的indexes
,我的意思是没有$array[0]
或$array[1]
。
You need to add a loop like below:您需要添加如下循环:
foreach($array as $product){
$sql = "INSERT INTO customer_order (productName, quantity, totalPrice, pax)";
$sql .= " VALUES ('{$product['name']}', '{$product['quantity']}', '{$product['price']}', '{$product['pax']}')";
if ($connect->query($sql)) {
$errorMsg = "Connection failed: " . $connect->connect_error;
$success = false;
}
if ($errorMsg != "Connection failed: ") {echo $errorMsg; $connect->close(); return;}
}
This will allow you to save each row data from shopping_cart
.这将允许您保存来自shopping_cart
的每一行数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.