我已经在php中创建了一个网站。我已经在其中提供了购物车功能。当用户单击添加到购物车按钮时,应将他们重定向到显示商品的购物车页面。下面是处理购物车代码的library.php:
<?php
// load database connection script
include("database_connection.php");
/*
* Tutorial: PHP MySQL Shopping cart
*
* Page: Application library
* */
class ShopingCart
{
protected $db;
function __construct()
{
$this->db = DB();
}
/**
* get products list
*
* @return array
*/
public function getProducts()
{
$query = "SELECT * FROM `entertainment`";
if (!$result = mysqli_query($this->db, $query)) {
exit(mysqli_error($this->db));
}
$data = [];
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
}
return $data;
}
/**
* get given product details
*
* @param [integer] $id
* @return array
*/
public function getProductDetails($id)
{
$id = mysqli_real_escape_string($this->db, $id);
$query = "SELECT * FROM `entertainment` WHERE `id` = '$id'";
if (!$result = mysqli_query($this->db, $query)) {
exit(mysqli_error($this->db));
}
$data = [];
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data['id'] = $row['id'];
$data['title'] = $row['title'];
$data['price'] = $row['vendor_price'];
$data['quantity'] = 1;
}
}
return $data;
}
/**
* Add new product into the cart
*
* @param [integer] $id
* @return void
*/
public function addToCart($id)
{
$product = $this->getProductDetails($id);
$isFound = false;
$i = 0;
实际上,我已经参考了互联网来制作此购物车。手推车工作正常。但是问题是我需要限制用户多次添加一个项目,用户只能添加一个项目一次,也就是说,该项目的数量应该仅为1,当用户尝试向一个项目添加更多次时,他们应该警惕。
谁能告诉我该如何解决?
慕码人8056858