2013-04-20 50 views
0

感谢您的支持!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 &pound; ' . $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方法做了。

谢谢大家。 :)

回答

0

只是一个猜测,因为有您的测试数据中没有的信息,但是这看起来可疑:

if (mysqli_affected_rows($dbc) == 1) { 

这检查,看看是否只有一行由查询返回。您的测试数据可能有2行至5行的一行,1行有多行?尝试将逻辑更改为:

if (mysqli_affected_rows($dbc) > 0) { 
+0

感谢您的回答@Ed ..这让我清楚了解我的代码正在发生什么。干杯! :) – GitKidd 2013-04-20 02:45:28

2

mysqli_real_escape_string($dbc,$string)用于清理输入。您不想在PHP中使用它之前使用它。它专门用于MySQL,这就是它需要数据库连接的原因。

$catno = $_GET['catno']; 

相反的,如果(这仍然是好的),你可以使用一个switch()声明:

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()就可以了,因为我们现在有一个变种($cat_id),将有可能插入到我们已经数据库的SQL语句设定自己。这通常是你会清理任何用户提供的变量以供插入的地方。

现在我们来看看这个var是否是正数。如果前面的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 &pound; ' . $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>'; 
} ?> 
+0

感谢@Absolute!这真的很有帮助,我肯定会记下这些提示。这是我从头开始的第一个定制项目,您的建议为我编写代码提供了更好的视角。再次感谢。 :) – GitKidd 2013-04-20 03:00:02