感謝您的支持!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