0

感谢您的光临!

我正在尝试为我的学校项目做一个电子商务网站。目的是用数据库中的内容填充我的网页,并且似乎没有记录 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 运行良好,并且在检索产品并将其显示在页面上时完美运行。(categories($catid) 2,3,4,5 - 如果点击了各自的链接,则工作正常)我的主要问题是所有条件 if/else 都记录了除第一个之外的值:

if ($sanitize == '10familia') {
$catid = 1;
}

没有为查询记录值 $catid= 1 以从数据库中的类别 1 中提取所有产品。

我不知道为什么这个特定的条件不起作用,但其他四个相同的工作..

注意:我刚开始使用 php,如果有更好的方法来执行 if-else,我深表歉意,我使用的是 if/elseif 方法。

谢谢你们。:)

4

2 回答 2

2

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 是否是正数。如果前一个 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>';
} ?>
于 2013-04-20T02:46:55.043 回答
0

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

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

这将检查查询是否只返回了一行。您的测试数据是否有可能在类别 2 到 5 中包含一行,而在类别 1 中包含多行?尝试将逻辑更改为:

if (mysqli_affected_rows($dbc) > 0) {
于 2013-04-20T02:19:31.753 回答