我过去几天一直在研究这个问题,这让我很疯狂,所以我想我会把问题放在这里。请注意,我还在学习php :)所以这是一个练习项目。
我目前正在创建一个模型注册页面,注册页面的用户可以注册,购买商品,并查看购买商品的其他人(这是故意的)。我目前的问题是,如果用户的项目已存在于数据库中,我想通过禁用“添加到购物车”按钮来禁止用户购买已购买的商品。基本上,买家只能从数据库购买1件物品,当他们试图通过去商店购买另一件物品时,他们将禁用“添加到购物车”按钮。
注意:当我最初做这个(大约2个月前)时,我只使用MySQL,但后来学习MySQLi,我开始(非常慢)转换它。数据库文件同时具有MySQL和MySQLi的连接,因此它允许我在学习和使用的同时使用它们。
<?php
session_start(); // Starts Session to carry Variables
include_once('THIS IS WHERE THE SQL FILE GOES. I REMOVED IT'); //Connects page to Database
/* Verifies user is logged in, if they are not redirect them to index.php */
if($loggedin != 1)
{
header("Location: index.php");
}
/* Category Feature. Allows Page to be Sorted by sort column */
if (isset($_POST['searchcatbtn'])){
}
include("header.php") /* Includes Header */
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<!-- Creates Products within a Div provided by a Style Sheet-->
<div id="products-wrapper">
<h1>Products</h1>
<div class="products">
<?php
//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$Qcategories = mysql_query("SELECT DISTINCT sort FROM events ORDER BY sort ASC;");//Get all data from the users table sorts list alphabetically by username
$results = $mysqli->query("SELECT * FROM events ORDER BY eventid");// WHERE sort = '$sort'
$remaining = $mysqli->query("SELECT * FROM orders");
$obj_r = $remaining->fetch_object(); // obj_r means objects remaining
$category = $obj->sort;
if ($results) {
//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="images/'.$obj->image.'"></div>';
echo '<div class="product-content"><h4>'.$obj->name.'</h4>';
echo '<div class="product-desc">'.$obj->description.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->price.' | ';
echo 'Items Left: '.$obj->max.' | ';
echo '<input type="hidden" style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />'; // OLD CODE TO DISPLAY QTY BOX: Qty: <input style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />';
echo '<button class="add_to_cart">Add To Cart</button>';
echo '</div></div>';
echo '<input type="hidden" name="product_code" value="'.$obj->name.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}
}
?>
</div>
<!-- Creates Cart within a Div provided by a Style Sheet-->
<div class="shopping-cart">
<h2>Your Shopping Cart</h2>
<?php
if(isset($_SESSION["products"]))
{
$total = 0; //sets default vaule of total
echo '<ol>';
foreach ($_SESSION["products"] as $cart_itm) /* Calls Session and references it as cart_itm. Creates items in the cart per item. If empty, shows empty cart*/
{
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>';
echo '<h3>'.$cart_itm["name"].'</h3>';
echo '<div class="p-code">Product code : '.$cart_itm["code"].'</div>'; //safe to delete this if you want
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '</ol>';
echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href="view_cart.php">Check-out!</a></span>'; /* total amount of cart */
echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'">Empty Cart</a></span>';
}
else
{
echo 'Your Cart is empty';
}
?>
<br><br>
<!-- Creates categories. Allows page to be sorted by Category -->
<h2>Categories</h2>
<form name="searchcat" action="store.php" method="POST">
<div align="center">
<select name="mydropdown">
<?php
while($row = mysql_fetch_array($Qcategories))//loops through $Query row by row
{
echo '<option value="' . $row['sort'] . '">' . $row['sort'] . '</option>'; //prints the categories from looping though the array
}
?>
<input type="submit" name="searchcatbtn" class="btn btn-success" value="Submit Edits" />
</form>
</select>
</div>
</div>
</body>
</html>