您的网站有1个问题我不知道该怎么做这个部分。我需要检查产品是否已购买。因此,如果用户已登录已购买该项目,则会显示其他内容(如果他们尚未购买该项目)。
使用此样本数据
products
productID, title, runTime, imageLarge, classification
1, noah, 128 minutes, noahLarge.jpg, M
2, left behind, 105 minutes, leftBehindLarge.jpg, MA 15+
3, green lantern, 133 minutes, greenLantern.jpg, M
invoice
invoiceID, memberID, purchaseDate
1,11,1/10/2014
2,13,15/10/2014
productsinvoice
invoiceID, productID
1, 2
1, 3
2, 1
2, 2
members
memberID, username, pass, first name, last name
11, bob, fwofefef, bob, marley
13, prius, f334ff43f, eski, martey
问题在于我的SQL查询和这段代码
$sql = "SELECT productID, title, price, imageSmall, imageLarge FROM products WHERE releaseDATE < NOW() ORDER BY releaseDate DESC LIMIT 0,4";
$result = mysqli_query($con, $sql) or die(mysqli_error($con)); //run the query
while ($row = mysqli_fetch_array($result))
{
if(!isset($_SESSION['user']))
{
echo '
<h4>$'. $row['price'] .'</h4>
<p><input type="button" value="Add to Cart" onclick="addtocart(' .$row['productID'] . ')" class="btn btn-primary" value="Add to cart"/></p>';
}
else {
echo '
<a href="../pages/movieWatch.php?productID='.$row['productID'].'" class="btn btn-success"><span class="glyphicon glyphicon-cloud"> Stream</a> <a href="#" class="btn btn-info"><span class="glyphicon glyphicon-hdd"> Download</a>';
}
} 所以基本上如果用户bob购买了产品ID 2,3,我希望它显示这两种产品的选项,但其余的仍然说添加到购物车并忽略$ _SESSION [&#39;用户&#39;]只是制作确定if条件是否有效。
答案 0 :(得分:0)
首先,您还应该从发票表中选择memberID。 所以你的查询应该是:
SELECT productID, title, price, imageSmall, imageLarge, memberID FROM products JOIN productsinvoice ON productsinvoice.productID=products.productID JOIN invoice ON productsinvoice.invoiceID=invoice.invoiceID WHERE releaseDATE < NOW() ORDER BY releaseDate DESC LIMIT 0,4;
第二,假设您还在会话变量中设置了memberID,请在if语句中添加另一个条件:
if(!isset($_SESSION['user'])){
if ($row['memberID'] != $_SESSION['user']['memberID']) {
echo '
<h4>$'. $row['price'] .'</h4>
<p><input type="button" value="Add to Cart" onclick="addtocart(' .$row['productID'] . ')" class="btn btn-primary" value="Add to cart"/></p>';
}
} else {
echo '
<a href="../pages/movieWatch.php?productID='.$row['productID'].'" class="btn btn-success"><span class="glyphicon glyphicon-cloud"> Stream</a> <a href="#" class="btn btn-info"><span class="glyphicon glyphicon-hdd"> Download</a>';
}