我有一个显示产品项目的 MySQL(目前不使用 PDO 或 MySQLi)查询。我决定使用带有基于 get_
和 isset
的代码片段的单个 PHP 页面,因此如果 page.php?id=1
或 page.php?cat=pills
将生成一个不同外观的页面。
除此之外,当我进入 page.php?id=1
时,我希望相关项目显示在底部,该项目与我拥有的代码一起使用,除了它还显示当前显示的 id这不是想要的效果。
Relevant | Tables
-------------------
product | short description price
category | short category expert
我的代码基本上查看当前选择的产品 ID 并查找缩写(类别的缩写形式)并将其定义为变量。
下一段代码显示与短类别匹配的所有产品。
代码
if(isset($_GET['id']))
{
$id=$_GET['id'];
$qry=mysql_query("SELECT * FROM product WHERE id='$id'", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
/* Fetching data from the field "category" and article id is transferred to product.php file */
while($row=mysql_fetch_array($qry))
{
$cat=$row['short'];
$qry=mysql_query("SELECT * FROM product WHERE short='$cat' order by product.id DESC", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
/* Fetching data from the field "title" */
while($row=mysql_fetch_array($qry))
{
echo "<a href='product.php?id=".$row['id']."'>";
echo "<div class='prod_item2'>";
echo "<h3>".$row['name']."</h3>";
echo "<img src='".$row['image']."' /><br>";
echo "<h2><img style='height:7%;' src='images/rs.png' />".$row['price']."</h2><br>";
echo "</div></a>";
}
}
}
我希望显示除产品中当前选定的项目之外的所有项目。
您可以在 indianskincare.co.in
中查看结果,然后单击左侧的类别之一,然后单击产品。除了相关产品显示所有产品(包括所选产品 ID)的部分外,一切都很完美。如何修改查询以排除当前的product.id。
最佳答案
在您的 SQL 查询中,您可以简单地排除获取具有当前 ID 的记录。
类似 $qry=mysql_query("SELECT * FROM Product WHERE Short='$cat' AND id!='$id' order by Product.id DESC", $con);
关于php - Mysql 查询排除当前显示的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28083506/