Php 禁用";“添加到购物车”;按钮,如果用户已经从数据库购买了该项目

Php 禁用";“添加到购物车”;按钮,如果用户已经从数据库购买了该项目,php,mysql,mysqli,cart,Php,Mysql,Mysqli,Cart,在过去的几天里,我一直在研究这个问题,这让我发疯,所以我想我会把这个问题带到这里来。请注意,我还在学习php:),所以这是一个实践项目 我目前正在创建一个模型注册页面,注册页面的用户可以注册、购买项目,并查看购买项目的其他人(这是有意的)。我当前的问题是,如果数据库中已经存在该用户的项目,我想通过禁用“添加到购物车”按钮来禁止该用户购买他们已经购买的项目。基本上,买家只能从数据库中购买一件商品,当他们试图通过去商店购买另一件商品时,“添加到购物车”按钮将被禁用 注意:当我最初做这个的时候(大约2

在过去的几天里,我一直在研究这个问题,这让我发疯,所以我想我会把这个问题带到这里来。请注意,我还在学习php:),所以这是一个实践项目

我目前正在创建一个模型注册页面,注册页面的用户可以注册、购买项目,并查看购买项目的其他人(这是有意的)。我当前的问题是,如果数据库中已经存在该用户的项目,我想通过禁用“添加到购物车”按钮来禁止该用户购买他们已经购买的项目。基本上,买家只能从数据库中购买一件商品,当他们试图通过去商店购买另一件商品时,“添加到购物车”按钮将被禁用

注意:当我最初做这个的时候(大约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.'">&times;</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>

购物车
产品

首先,您将MySQL API与
$Qcategories=MySQL\u query
混合,然后在
MySQL\u fetch\u数组($Qcategories)
中使用
$results=$mysqli->query
。这些不同的API彼此不兼容。从连接到查询,您需要使用相同的API函数。当我最初这样做时(大约2个月前),我只使用MySQL,但在学习MySQL之后,我开始(非常缓慢)转换它。数据库文件同时具有MySQL和MySQLi的连接,因此它允许我在学习和学习的同时利用这两种连接。我会编辑这篇文章来说明这一点。你不应该两者兼而有之,强烈不推荐,这是一种不好的做法,你只需要继续练习坏习惯。谢谢你提到这一点。正如我前面提到的,我现在也在慢慢地将所有内容转换为MySQLi不客气。现在,试着回答这个问题;这一切归结为条件语句
if(条件){do something}else{do something}
。如果所讨论的会话数组已被设置和使用,则对其使用条件语句。因为您使用的是DB,所以还可以将会话数组写入DB,并检查该行是否与您希望为其提供的条件匹配,或者检查是否匹配。