Php 显示产品类别

Php 显示产品类别,php,mysql,e-commerce,categories,Php,Mysql,E Commerce,Categories,我正在为一家企业创建一个简单的网站来展示他们的产品。在索引页上,我显示了3种最新产品。我现在需要做的是有一个分类页面,人们可以去查看某个类别的产品。我尝试从我的索引页面修改代码,但是产品没有显示在我的category.php上 我有一个包含不同类别的product_list.php,当选择其中一个类别时,我将尝试使用所选类别中的产品加载category.php 我所拥有的: product_list.php 在category.php中,我试图创建一个名为$dynamicList的变量,其中包含

我正在为一家企业创建一个简单的网站来展示他们的产品。在索引页上,我显示了3种最新产品。我现在需要做的是有一个分类页面,人们可以去查看某个类别的产品。我尝试从我的索引页面修改代码,但是产品没有显示在我的category.php上

我有一个包含不同类别的product_list.php,当选择其中一个类别时,我将尝试使用所选类别中的产品加载category.php

我所拥有的:

product_list.php 在category.php中,我试图创建一个名为$dynamicList的变量,其中包含所有将dock作为一个类别的产品。但当我回显$dynamicList时,不会呈现任何内容

<?php 
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$category=$_GET['category'];
$sql = mysql_query("SELECT * FROM products WHERE category='$category' LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
        <tr>
          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="100" height="64" border="1" /></a></td>
          <td width="83%" valign="top">' . $product_name . '<br />
            $' . $price . '<br />
            <a href="product.php?id=' . $id . '">View Product Details</a></td>
        </tr>
      </table>';
    }
} else {
    $dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">

</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $category; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
  <table width="100%">
      <tr>
        <td valign="top"><p><?php echo $dynamicList;?><br />
          </p>
          <p>&nbsp;</p>
        <p>&nbsp;</p>          <h2>&nbsp;</h2></td>
      </tr>
  </table>
  <p>&nbsp;</p>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>

EDIT:$dynamicList嵌套在条件语句中。只要写

$dynamicList = '';
在你询问之前

页面上的当前输出是什么?查询中返回的行数是否为0? 您还应该使用mysql\u real\u escape\u字符串清理$category输入

WHERE category ='".mysql_real_escape_string($category)."' ...

EDIT:$dynamicList嵌套在条件语句中。只要写

$dynamicList = '';
在你询问之前

页面上的当前输出是什么?查询中返回的行数是否为0? 您还应该使用mysql\u real\u escape\u字符串清理$category输入

WHERE category ='".mysql_real_escape_string($category)."' ...
应该是

$sql = mysql_query("SELECT * FROM products WHERE category='".$category."' LIMIT 6");
这是目前我能发现的唯一错误

应该是

$sql = mysql_query("SELECT * FROM products WHERE category='".$category."' LIMIT 6");
这是我目前能发现的唯一错误

您必须首先使用此函数清理字符串:

public static function real_escape($string)
{
    if (null != $string) {
        $string = (get_magic_quotes_gpc()) ? $string : addslashes($string);
        return mysql_real_escape_string($string);
    }
    return $string;
}
或者最好使用第三方脚本,如

您必须首先使用此函数清理字符串:

public static function real_escape($string)
{
    if (null != $string) {
        $string = (get_magic_quotes_gpc()) ? $string : addslashes($string);
        return mysql_real_escape_string($string);
    }
    return $string;
}

或者最好使用第三方脚本,如

无法修复它,还应进行清理。$category包含一个字符串,并且必须在查询中用单引号括起来。如果您担心变量插值,那么整个查询字符串由双引号限定,因此$category将被正确解释。将其用单引号括起来是非常不安全的,当我尝试这样做时,它不会起作用everytime@MrRap,你能给我们看一个SQL语句的例子吗?它可以将字符串值插入字符串类型的字段char,varchar,文本,而不需要在值周围加引号。非常感谢大家!我让它工作了。这些评论对学习如何编写好代码非常有帮助!$category包含一个字符串,并且必须由查询中的单引号限定。如果您担心变量插值,那么整个查询字符串由双引号限定,因此$category将被正确解释。将其用单引号括起来是非常不安全的,当我尝试这样做时,它不会起作用everytime@MrRap,你能给我们看一个SQL语句的例子吗?它可以将字符串值插入字符串类型的字段char,varchar,文本,而不需要在值周围加引号。非常感谢大家!我让它工作了。这些评论对学习如何编写好代码非常有帮助!顺便说一句,您应该用%20转义src字符串中的空格。这是您的全部代码吗?当前形式的if/then语句似乎涵盖了所有可能性。顺便说一句,您应该使用%20对src字符串中的空格进行转义,这是您的全部代码吗?当前形式的if/then语句似乎涵盖了所有可能性。行数是否为0如果是,则输出必须为我们的商店中尚未列出任何产品。但问题表明,输出为空-这在上面的代码中也不可能为真?!奇怪。行数=0吗?如果是这样,输出肯定是我们的商店中还没有列出任何产品。但是问题表明,输出是空的-这在上面的代码中也不可能是真的?!奇怪的