感谢您的支持!MySQL查询并获取id值
我想为我的学校项目做一个电子商务网站。目的是用数据库中的内容填充我的网页,并且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工作正常,并在检索的产品和在网页上展示它完美。 (类别($ catid)2,3,4,5 - 工作正常,如果他们各自的链接被点击)我主要的问题是,所有条件if/else记录的值除了第一个:
if ($sanitize == '10familia') {
$catid = 1;
}
值为$ catid = 1的记录不会被查询以从数据库中的类别1中提取所有产品。
我不知道为什么这个特定条件将无法正常工作,但其他四个是相同的作品..
注:我刚开始PHP和我道歉,如果有更好的方式,如果做 - 我用if/elseif方法做了。
谢谢大家。 :)
感谢您的回答@Ed ..这让我清楚了解我的代码正在发生什么。干杯! :) – GitKidd 2013-04-20 02:45:28