使用复选框搜索mysql?

时间:2015-02-25 14:33:05

标签: php mysql checkbox

我一直在努力工作几个小时但无济于事。

我正在尝试使用PHP和mysql为我的网站创建高级搜索功能。

高级搜索中只有复选框。

复选框看起来像这样:

<form action="search.php"  method="POST" id="myForm"/>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>&nbsp;</td>
    <td><input type="checkbox" name="keyword[]"  value="small"/></td>
    <td>Small</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="checkbox" name="keyword[]"  value="large"/></td>
    <td>Large</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="checkbox" name="keyword[]"  value="xlarge"/></td>
    <td>X-Large</td>
  </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input type="checkbox" name="keyword[]"  value="xxlarge"/></td>
    <td>XX-Large</td>
  </tr>

  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
      <tr>
    <td>&nbsp;</td>
    <td><input type="checkbox" name="keyword[]"  value="red"/></td>
    <td>Red</td>
  </tr>
   <tr>
    <td>&nbsp;</td>
    <td><input type="checkbox" name="keyword[]"  value="black"/></td>
    <td>Black</td>
  </tr>
     <tr>
    <td>&nbsp;</td>
    <td><input type="checkbox" name="keyword[]"  value="white"/></td>
    <td>White</td>
  </tr>
</table>

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

我的MYSQL数据库如下所示:

产品表:

id  product_name  price  
5     shirt        20
6     shoe         70

我有一个属性表,用于存储产品的颜色和大小。

此表如下所示:

idc  id  product_name  colors   sizes 
1     5     shirt        red     
2     5     shirt        back
3     5     shirt        white
4     5     shirt                small
5     5     shirt                medium
6     5     shirt                Large
7     6     shoe         brown
8     6     shoe         black
9     6     shoe                   5 
10    6     shoe                   6
11    6     shoe                   7

在我的search.php页面中,我有这段代码:

include "config/connect.php"; 
$searchList = "";


foreach($_POST['keyword'] as $c){
$sql ="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id= ATTRIBUTES.id
WHERE (
     ATTRIBUTES.size LIKE '%".$c."%' OR
     ATTRIBUTES.color LIKE '%".$c."%'
      )";
$query = mysqli_query($db_conx, $sql);
}

//var_dump($query);

$productCount = mysqli_num_rows($query);
$i=0; // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
             $id = $row["id"];
             $product_name = $row["product_name"];
             $searchList .= ''.$product_name.'';
    }
}

上面的代码没有返回任何内容,我不断收到此错误:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

这意味着我的SQL查询后出现了问题!

所以我做了var_dump($query);,这给了我bool(fales)

没有多大帮助,因为这意味着与第一个错误相同!

有人可以帮我解决这个问题吗?

任何建议和帮助将不胜感激。

编辑:

我已将代码更改为:

<?php
error_reporting(-1);
ini_set('display_errors', 'On'); 
include "config/connect.php";
$searchList = "";
$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
if(isset($_POST['submit'])){
    if(isset($_POST['keyword'])){
        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                $sql .= $clause."`".$c."` LIKE '%{$c}%'";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }   $query = mysqli_query($db_conx, $sql);
        }
    }
//echo $sql;//Remove after testing
}
//var_dump($query);

$productCount = mysqli_num_rows($query);
$i=0; // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
             $id = $row["id"];
             $product_name = $row["product_name"];
             $searchList .= ''.$product_name.'';
    }
}
?>
<?php echo $searchList; ?>

但上面的代码给出了以下错误:

Notice: Undefined variable: query in on line 23 
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given on line 23 
我正朝着正确的方向前进吗?

编辑:

此代码从MYSQL数据库中返回数据,但信息错误!

$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
if(isset($_POST['submit'])){
    if(isset($_POST['keyword'])){
        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                $sql .= $clause."`sizes` LIKE '%{$c}%'";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }
        }
        // Run query outside of foreach loop so it only runs one time.
        $query = mysqli_query($db_conx, $sql);
        // Check that the query ran fine.
        if (!$query) {
            print "ERROR: " . mysqli_error($db_conx);
        } else {
            // Use $query inside this section to make sure $query exists.
            $productCount = mysqli_num_rows($query);
            $i=0; // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
                   $id = $row["id"];
                   $product_name = $row["product_name"];
                   $searchList .= ''.$product_name.'';
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试将其更改为此内容并查看您获得的内容:

<?php
error_reporting(-1);
ini_set('display_errors', 'On'); 
include "config/connect.php";
$searchList = "";
$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
if(isset($_POST['submit'])){
    if(isset($_POST['keyword'])){
        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                ##NOPE##$sql .= $clause."`".$c."` LIKE '%{$c}%'";
                $sql .= $clause . " (ATTRIBUTES.sizes LIKE '%$c%' OR ATTRIBUTES.colors LIKE '%$c%')";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }
        }
        print "SQL Query: $sql<br />"; //<-- Debug SQl syntax.
        // Run query outside of foreach loop so it only runs one time.
        $query = mysqli_query($db_conx, $sql);
        var_dump($query); //<-- Debug query results.
        // Check that the query ran fine.
        if (!$query) {
            print "ERROR: " . mysqli_error($db_conx);
        } else {
            // Use $query inside this section to make sure $query exists.
            $productCount = mysqli_num_rows($query);
            $i=0; // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
                   $id = $row["id"];
                   $product_name = $row["product_name"];
                   $searchList .= ''.$product_name.'';
                }
            }
        }
    }
}
?>
<?php echo $searchList; ?>

已编辑:修正了错误的列名称。 也许是这样的事情?