我正在尝试在cart table
中插入数据,但实际上发生了一些可疑的事情......首先,在我点击“添加到购物车”和我实际看到的那一刻之间有一个巨大的延迟购物车表中的数据。其次,我无法同时添加多个项目。当脚本启动成功消息时,一切似乎都没问题。但是,当我单独运行add_cart.php
时,我会在页面顶部看到此?
。
add_cart.php
<?php
ob_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/MyProjectName/core/init.php';
$product_id = isset($_POST['product_id'])? sanitize($_POST['product_id']):' ';
$size = isset($_POST['size'])? sanitize($_POST['size']):' ';
$available = isset($_POST['available'])? sanitize($_POST['available']):' ';
$quantity = isset($_POST['quantity'])? sanitize($_POST['quantity']):' ';
$item = array();
$item[] = array(
'id' => $product_id,
'size' => $size,
'quantity' => $quantity,
'available' => $available,
);
$domain =($_SERVER['HTTP_HOST'] != 'localhost')?'.'.$_SERVER['HTTP_HOST']:false;
$query = $db->query("SELECT * FROM product WHERE id = '{$product_id}'");
$product = mysqli_fetch_assoc($query);
$_SESSION['success_flash'] = $product['prod_name']. ' was added to your cart.';
//check if the cart cookie exists
if (is_array($cart_id != ' ')) {
$cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
$cart = mysqli_fetch_assoc($cartQ);
$previous_items = json_decode($cart['items'],true);
$item_match = 0;
$new_items = array();
foreach($previous_items as $pitem){
if ($item[0]['id'] == $pitem['id'] && $item[0]['size'] == $pitem['size']) {
$pitem['quantity'] = $pitem['quantity'] + $item[0]['quantity'];
if ($pitem['quantity'] > $available) {
$pitem['quantity'] = $available;
}
$item_match = 1;
}
$new_items[] = $pitem;
}
if ($item_match != 1) {
$new_items = array_merge($item,$previous_items);
}
$items_json = json_encode($new_items);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("UPDATE cart SET items = '{$items_json}', expire_date = '{$cart_expire}' WHERE id = '{cart_id}'");
setcookie(CART_COOKIE,'',1,"/",$domain,false);
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}else{
// add to databse and set cookie
$items_json = json_encode($item);
$cart_expire = date("Y-m-d H:i:s",strtotime("+30 days"));
$db->query("INSERT INTO cart (items,expire_date) VALUES ('{$items_json}','{$cart_expire}')");
$cart_id = $db->insert_id;
setcookie(CART_COOKIE,$cart_id,CART_COOKIE_EXPIRE,'/',$domain,false);
}
?>
的init.php
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "mydatabasename");
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if(mysqli_connect_errno()){
echo "database failed to connect with following errors:". mysqli_connect_error();
die();
}
session_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/MyProjectName/config.php';
require_once BASEURL.'helpers/helpers.php';
$cart_id = '';
if(isset($_COOKIE[CART_COOKIE])){
$cart_id = sanitize($_COOKIE[CART_COOKIE]);
}
if(isset($_SESSION['SBUser'])){
$user_id = $_SESSION['SBUser'];
$query = $db->query("SELECT * FROM users WHERE id ='$user_id ' ");
$user_data = mysqli_fetch_assoc($query);
$fn = explode(' ', $user_data['full_name']);
$user_data['first'] = $fn[0];
$user_data['last'] = $fn[1];
}
if(isset($_SESSION['success_flash'])){
echo '<div class="bg-success"><p class="text-success text-center">'.$_SESSION['success_flash'].'</p></div>';
unset($_SESSION['success_flash']);
}
if(isset($_SESSION['error_flash'])){
echo '<div class="bg-danger"><p class="text-danger text-center">'.$_SESSION['error_flash'].'</p></div>';
unset($_SESSION['error_flash']);
}