仅显示数据库中的特定类别(PHP / SQL)

时间:2010-09-21 01:30:58

标签: php sql

从下拉菜单中,用户可以选择:查看全部,运动,连衣裙或凉鞋。我正在创建一个功能,如果用户选择运动 - 只有产品类型'运动',将只显示数据库中的运动项目。

现在,因为我的代码是如何编写的,如果用户选择'Athletic',他们会看到运动项目,还有数据库中的所有其他产品,因为函数showAllProducts被调用。

我不确定如何写,如果用户选择特定的产品类型,则只会显示该产品类型。

if (isset($_SESSION['valid_user']))
      {
          //echo "I am in the if statement of the session";
        echo 'You are logged in as: '.$_SESSION['valid_user'].' <br />';
        showAllProducts();

      } else {
        echo "I am not setting the session variable";
        //die;
      }

    $userCat = getUserCategory();
    orderByCategory($userCat);

  //function athleticCategory ---------------------------------------------     
    function athleticCategory() {

            echo "I am in the athletic function" . "<br/>";

            $con = getConnection();
            $sqlQuery = "SELECT * from Products
                        WHERE ProductType='Athletic'";

             // Execute Query -----------------------------           
            $result = mysqli_query($con, $sqlQuery);
                if(!$result) {
                    echo "Cannot do query" . "<br/>";
                    exit;
                }

                $row = mysqli_fetch_row($result);
                $count = $row[0];

                if ($count > 0) {
                    echo "Query works" . "<br/>";
                } else {
                    echo "Query doesn't work" ."<br/>";
                }

              // Display Results -----------------------------
                $num_results = mysqli_num_rows($result);

                for ($i=0; $i<$num_results; $i++) {
                    $row = mysqli_fetch_assoc ($result);
                   // print_r($row);
                  echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Image']).'" />';
                  echo "Price: " . stripslashes($row['Price']);
                }

            }

下拉菜单

    <form action="register_script.php" name="frm" method="post">
        <select name="category" id="category">
            <option value="viewall">View All</option>
            <option value="dress">Dress</option>
            <option value="athletic">Athletic</option>
            <option value="sandals">Sandals</option>
       </select>

      <input type="submit" value="Go" />

  </form>

已编辑的代码:

 $sqlQuery = "SELECT * from Products";

    if($pUserCat == "athletic") {
       $sqlQuery = "SELECT * from Products
                    WHERE ProductType='athletic'";
    } elseif ($pUserCat == "dress") {
        $sqlQuery = "SELECT * from Products
                    WHERE ProductType='dress'";
    } elseif ($pUserCat == "sandals") { 
            $sqlQuery = "SELECT * from Products
                    WHERE ProductType='sandals'";
    } elseif ($pUserCat == "viewall") {                 
       $sqlQuery = "SELECT * from Products";
    }

2 个答案:

答案 0 :(得分:1)

创建一个函数,接受一个参数即类别名称,并使用默认提示为0

function categoryList($cat=false){
if($cat)
   $sqlQuery = "SELECT * from Products
                        WHERE ProductType={$cat}";
else
   $sqlQuery = "SELECT * from Products";
//do other stuff of Reading option 
} 

答案 1 :(得分:1)

设置“查看全部”表单选项,如下所示:

<option value="">View All</option>

您可以原样使用

  if (isset($_POST['category']))
    $category = $_POST['category'];

  $sqlQuery = "SELECT * from Products";
  if ( ! empty($category)) {
    if (get_magic_quotes_gpc()) {
      $category = stripslashes($category);
    }
    if ( ! is_numeric($category)) {
      $category = "'" . mysql_real_escape_string($category) . "'";
    }
    $sqlQuery .= " WHERE ProductType='{$category}'";
  }

它具有基本的安全功能,因此人们无法将恶意SQL注入您的脚本。

如果您在没有任何类别的情况下调用该函数,则会假定您要显示所有值。

你不需要检查是否每个案例,然后根据它来编写sqlQuery,只要你使用相同的 <option value="xxx">,因为你的类别被调用分贝。