感谢您的光临!
我正在尝试为我的学校项目制作一个电子商务网站。目的是用数据库中的内容填充我的网页,并且似乎没有记录 1 个特定条件,以便我能够从数据库中提取数据以在网站上呈现:
在我的主页的侧边栏上有这样的代码:这会导致一个 php 页面根据产品类别检索所有产品
<ul class="left_menu">
<li class="odd"><a href="categories.php?catno=10familia">Familia Originals</a></li>
<li class="even"><a href="categories.php?catno=20familia">Ready to Cook</a></li>
<li class="odd"><a href="categories.php?catno=30familia">Siomai and Buns</a></li>
<li class="even"><a href="categories.php?catno=40familia">Pork Snacks</a></li>
<li class="odd"><a href="categories.php?catno=50familia">Ready Made Dishes</a></li>
</ul>
我有这个代码/页面,将通过使用 (id)catno= 来引用应显示的类别,根据产品类别来显示产品。
// Sanitize the $_GET['catno'] and match with the correct category:
$sanitize = mysqli_real_escape_string($dbc, $_GET['catno']);
//match cases according to the product category
if ($sanitize == '10familia') {
$catid = 1;
} elseif ($sanitize == '20familia') {
$catid = 2;
} elseif ($sanitize == '30familia') {
$catid = 3;
} elseif ($sanitize == '40familia') {
$catid = 4;
} elseif ($sanitize == '50familia') {
$catid = 5;
} else {
$cat_error = '<div class="center"><h2>There are no products in this category. Please try again later.</h2></div>';
}
?>
<div class="center_content">
<div class="center_title_bar">Latest Products</div>
<div class="scroll_box_tall">
<?
$query = "SELECT product_id, name, price, thumbnail FROM products WHERE category_id = '$catid' ORDER BY product_id ASC";
$request = mysqli_query($dbc, $query);
if (mysqli_affected_rows($dbc) == 1) {
while ($row = mysqli_fetch_array($request, MYSQLI_NUM)) {
$item_id = $row[0]; // Assign product_id to $item_id to be passed on the href
$item_name = $row[1]; // Assign name to var $item_name
$item_price = $row[2]; // Assign price to var $item_price
$item_thumb = $row[3]; // Assign thumbnail to $item_thumb
// echo item name
$div1 = '<div class="prod_box"><div class="top_prod_box"></div><div class="center_prod_box"><div class="product_title">' . $item_name . '</div>';
// echo the thumbnail
$div2 = '<div class="product_img"><a href="show_product.php?idno=' . $item_id . '"><img src="product/thumb/' . $item_thumb . '" alt="' . $item_name . '"/></a></div>';
// echo the price
$div3 = '<div class="prod_price"><span class="price">RRP £ ' . $item_price . '</span></div></div><div class="bottom_prod_box"></div></div>';
echo "$div1$div2$div3";
}
} else { // Say an error message if there is no products in the category or the query is unsuccessful
echo '<div class="center"><h2>There are no products in this category. Please try again later.</h2></div>';
} ?>
</div>
条件 if/else 工作正常,并且可以完美地检索产品并将其显示在页面上。 (categories($catid) 2,3,4,5 - 如果单击各自的链接,则工作正常)我的主要问题是,除了第一个条件之外,所有条件 if/else 都会记录值:
if ($sanitize == '10familia') {
$catid = 1;
}
未记录值 $catid= 1 以用于从数据库中的类别 1 中提取所有产品的查询。
我不知道为什么这个特定的条件不起作用,但其他四个相同的条件却起作用..
注意:我刚刚开始 php,如果有更好的方法来执行 if-else,我深表歉意,并且我使用 if/elseif 方法来完成它。
谢谢大家。 :)
最佳答案
mysqli_real_escape_string($dbc,$string)
用于清理输入。在 PHP 中使用它进行任何操作之前,您不想使用它。它专门用于 MySQL,这就是它需要数据库连接的原因。
$catno = $_GET['catno'];
您可以使用 switch()
语句来代替 if(这仍然可以):
switch($catno){
case '10familia':
$catid = 1;
break;
case '20familia':
$catid = 2;
break;
case '30familia':
$catid = 3;
break;
case '40familia':
$catid = 4;
break;
case '50familia':
$catid = 5;
break;
default:
$cat_error = '<div class="center"><h2>There are no products in this category. Please try again later.</h2></div>';
$cat_id=-1;
echo $cat_error;
}
为了简洁起见,我将在此处省略代码的 HTML 部分。
由于我们甚至没有使用 $catno
来查询数据库,因此无需对其执行 mysqli_real_escape_string()
因为我们现在有一个 var ($cat_id
),它可能会插入到我们自己设置的数据库的 SQL 语句中。通常,您可以在此处清除任何用户提供的用于插入的变量。
现在我们要看看这个 var 是否为正。如果之前的变量没有正确传递,我们预计它会是负数。
if($cat_id > -1){
作为一种偏好,我喜欢在尝试让 MySQL 跳过很多环节来查看连接是否会失败之前,先拉出一个计数查询来检查简单的事情,比如表是否存在。这是一个额外的查询,但它可以在传递错误语句的大量查询上节省大量开销。如果您不追寻大量跨表故障,那么查找起来也更容易。
$count = mysql_result(mysqli_query($dbc, "select count(*) from products where category_id='$catid'"),0);
if ($count == 1){
$query = "SELECT product_id, name, price, thumbnail FROM products WHERE category_id = '$catid' ORDER BY product_id ASC";
$request = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($request)) {
在这些上,我喜欢实际设置我正在拉取的列的名称。一旦您进入更大的表,这将会有所帮助,这样您就不必弄清楚第 45 列的作用。此外,它还使那些可能与您一起从事此工作或继承该项目的人变得更容易。
$item_id = $row['product_id']; // Assign product_id to $item_id to be passed on the href
$item_name = $row['name']; // Assign name to var $item_name
$item_price = $row['price']; // Assign price to var $item_price
$item_thumb = $row['thumbnail']; // Assign thumbnail to $item_thumb
// echo item name
需要养成的习惯是在 div 之类的内容之后创建一个新行,这样您就可以在前端对 HTML 代码进行故障排除。
$nl = "\n";
$div1 = '<div class="prod_box">'.$nl.'<div class="top_prod_box"></div>'.$nl.'<div class="center_prod_box">'.$nl.'<div class="product_title">'.$item_name.'</div>'.$nl;
// echo the thumbnail
$div2 = '<div class="product_img"><a href="show_product.php?idno='.$item_id.'"><img src="product/thumb/'.$item_thumb.'" alt="'.$item_name.'"/></a></div>'.$nl;
// echo the price
$div3 = '<div class="prod_price"><span class="price">RRP £ ' . $item_price . '</span></div>'.$nl.'</div>'.$nl.'<div class="bottom_prod_box"></div>'.$nl.'</div>'.$nl;
echo "$div1$div2$div3";
}
} else { // Say an error message if there is no products in the category or the query is unsuccessful
echo '<div class="center"><h2>There are no products in this category. Please try again later.</h2></div>';
} ?>
关于php - MySQL查询并获取id值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115778/