如何将所有可能的WHERE选项发送到mysqli?

时间:2015-05-16 11:16:00

标签: php html mysqli

大家好我通过表单/选择/输入发送3 var到mysqli_fetch,它创建了另一个var的div并放置了与name.jpg具有相同id的图像。

问题:如何将所有选项的颜色设置为将所有可能的颜色发送到$ color_val?

enter image description here

<form action="produkt.php" method="post">
<!-- COLOR-->
<li>
<img class="img_search_bar" src="../img/search/color.jpg" />
    <select id="color" name="color">
      <option value="all" selected="selected">ALL</option>
      <option value="blue">blue</option>
      <option value="red">red</option>
      <option value="black">black</option>
      <option value="white">white</option>
    </select>
    </li>
  <!-- 2 more like this for size and type-->
 <INPUT TYPE="submit" name="submit" /></a>  <!-- refresh page-->
 </form>


<?php
if(isset($_POST['submit'])){
     // Storing Selected Value In Variable
   $color_val = $_POST['color'];
   $size_val = $_POST['size']; 
   $type_val = $_POST['type'];  

// Displaying Selected Value
   echo "COLOR :" .$color_val;  
   echo "<br>SIZE :" .$size_val;
   echo "<br>TYPE :" .$type_val;

//making connection to database
   $link = mysqli_connect("localhost", "root","" ,"test");
//selecting query from database where var = select var
   $query = "SELECT * FROM testtable WHERE color = '$color_val' AND size ='$size_val' AND type = '$type_val'";

   if ($result = mysqli_query($link, $query)){

/* fetch associative array */
       while ($row = mysqli_fetch_assoc($result)) {
       printf ("
         <div id='produkt_table'>
          <img class='produkt_ikon' src='../img/produkt/ikon/%s.jpg'>
          <h3> name : name </h3>
          <h4> Prize : %s </h4>
          <h5> Stock : %s </h5>
         </div>         
    ", $row["id"], $row["prize"], $row["stock"]);
    }

/* free result set */
     mysqli_free_result($result);
   }
}
/* close connection  mysqli_close($link);*/
?>

3 个答案:

答案 0 :(得分:2)

大胖警告:你的代码不守,我的榜样就是这样!阅读更多内容!

您的描述非常模糊,但我试一试。

  

$ query =&#34; SELECT * FROM testtable WHERE color =&#39; $ color_val&#39; AND size =&#39; $ size_val&#39; AND type =&#39; $ type_val&#39;&#34;;

如果要在$color_val === 'ALL'时匹配所有颜色,则必须使查询成为条件。

注意:此代码和您的代码是INSECURE

if ($color_val === 'ALL' && empty($color_val)) {
  $query = "SELECT * FROM testtable WHERE size ='$size_val' AND type = '$type_val'";
} else {
  $query = "SELECT * FROM testtable WHERE color = '$color_val' AND size ='$size_val' AND type = '$type_val'";
}

关于那个安全事件

您的代码对SQL注入是开放的,我建议您开始阅读PHP Manual或只是google for it

您无法验证,您无法逃脱,您也可以将密码泄露给您的数据库。

答案 1 :(得分:2)

以下代码应该这样做。为了得到这个,我对你的代码进行了细微的修改。你现在也可以优化它。

更改

在html部分我已将选项'all'的值更改为1.这是为了轻松检查后端的条件。

变量$ Query_appender用于存储部分查询。如果有特定的颜色通过,它将携带其他只携带1的条件。  现在我已修改您的查询以将颜色条件更改为最后并将该值附加到变量中。

这就是说,如果它只是颜色,你可以检查post变量的值,并在条件语句中写两个不同的查询。

<form action="produkt.php" method="post">
<!-- COLOR-->
<li>
<img class="img_search_bar" src="../img/search/color.jpg" />
    <select id="color" name="color">
      <option value="1" selected="selected">ALL</option>
      <option value="blue">blue</option>
      <option value="red">red</option>
      <option value="black">black</option>
      <option value="white">white</option>
    </select>
    </li>
  <!-- 2 more like this for size and type-->
 <INPUT TYPE="submit" name="submit" /></a>  <!-- refresh page-->
 </form>


<?php
if(isset($_POST['submit'])){
     // Storing Selected Value In Variable
   $color_val = $_POST['color'];
   $size_val = $_POST['size']; 
   $type_val = $_POST['type'];
   $Query_appender = '1';
   if($color_val!=1)
       {
        $query_appender = "color = '$color_val'";}

// Displaying Selected Value
   echo "COLOR :" .$color_val;  
   echo "<br>SIZE :" .$size_val;
   echo "<br>TYPE :" .$type_val;

//making connection to database
   $link = mysqli_connect("localhost", "root","" ,"test");
//selecting query from database where var = select var
   $query = "SELECT * FROM testtable WHERE size ='$size_val' AND type = '$type_val' AND ".$Query_appender;

   if ($result = mysqli_query($link, $query)){

/* fetch associative array */
       while ($row = mysqli_fetch_assoc($result)) {
       printf ("
         <div id='produkt_table'>
          <img class='produkt_ikon' src='../img/produkt/ikon/%s.jpg'>
          <h3> name : name </h3>
          <h4> Prize : %s </h4>
          <h5> Stock : %s </h5>
         </div>         
    ", $row["id"], $row["prize"], $row["stock"]);
    }

/* free result set */
     mysqli_free_result($result);
   }
}
/* close connection  mysqli_close($link);*/
?>

答案 2 :(得分:0)

尝试将其替换为$query = "SELECT * FROM testtable WHERE size ='$size_val' AND type = '$type_val' AND ".$Query_appender;

$query = "SELECT * FROM testtable";
if($color_val !='all' || $size_val !='all' || $type_val !='all')
{
    $query_arr = array();
    if($color_val !='all')
    {
        $query_arr[] = "color = '$color_val'";
    }
    if($size_val !='all')
    {
        $query_arr[] = "size ='$size_val'";
    }
    if($type_val !='all')
    {
        $query_arr[] = "type = '$type_val'";
    }

    $query .=" where ".implode(" AND ", $query_arr);
}