0

我正在创建一个搜索肉类包装数据库的多形式搜索框。搜索仅在用户特定时才有效,我希望他们能够搜索数据库,而不必从下拉框中选择特定选项,从而限制搜索次数。

是否可以在下拉框中添加一个选项,例如“任何”,如果用户选择它,SQL 查询会忽略它吗?那么它只会搜索与文本表单中的结果匹配的结果吗?

这是HTML和PHP:

 <body>

  <form action="form3.php" method="post"> 
    <label for ="description">Description:</label>
    <input type="text" name="descrip" /><br />

    <label for ="trayheight">Trayheight:</label>
    <input type="text" name="height" /> <br /> 

    <label for ="traywidth">Traywidth:</label>
    <input type="text" name="width" /> <br />

    <label for ="traydepth">Traydepth:</label>
    <input type="text" name="depth" /> <br />

         <label for="trayrange">Trayrange: </label>
                <select name="trayrange">
                    <option value="Other">Any</option>

                    <option value="BBQ">BBQ</option>

                    <option value="Dessert">Dessert</option>

                    <option value="Display">Display</option>

                    <option value="Meat">Meat</option>  

                    <option value="Microwave">Microwave</option>

                    <option value="Party">Party</option>

                    <option value="Salad/Wet Pasta">Salad/Wet Pasta</option>

                    <option value="Snacks">Snacks</option>

                    <option value="Standard">Standard</option>

                </select>

        <label for ="traytype">Traytype: </label> 
            <select name="traytype">
                    <option value="Other">Any</option>

            <option value="Open">Open</option>

            <option value="Cavitised">Cavitised</option>

                    <option value="Lid">Lid</option>

                    <option value="Tray">Tray</option>

                    <option value="Coallition">Coallition</option>

                    <option value="Bowl">Bowl</option>

                    <option value="Hinge pack">Open</option>

                    <option value="Pot">Pot</option>

                    <option value="Base & Lid">Base and Lid</option>

                    <option value="Rectangular">Rectangular</option>

                    <option value="Specalist">Specialist</option>
                </select><br />

        <label for="trayshape">Trayshape: </label>
            <select name="trayshape">
                    <option value="Other">Any</option>

            <option value="Rectangular">Rectangular</option>

                <option value="Oval">Oval</option>

                <option value="Square">Square</option>

                    <option value="Insert">Insert</option>

                    <option value="Round">Round</option>

                    <option value="Open">Open</option>
        </select><br />
        <input type="submit" value="Submit" /> 
    </form> 

        </body>

(也许是这样,Any 选项只会使代码忽略搜索的保管箱)

PHP:

      <body>

    <?php
         $con = mysql_connect ("localhost", "root", "");
         mysql_select_db ("delyn_db", $con);

    if (!$con)
        { 
        die ("Could not connect: " . mysql_error());
        }

            $descrip = mysql_real_escape_string($_POST['descrip']); 
            $height = mysql_real_escape_string($_POST['height']);
            $width = mysql_real_escape_string($_POST['width']);
            $depth = mysql_real_escape_string($_POST['depth']);

            $varRange = $_POST['trayrange'];
            $varType = $_POST['traytype'];
            $varShape = $_POST['trayshape'];

    $sql = "SELECT * FROM delyn WHERE description LIKE '%".$descrip."%' AND trayheight
                    LIKE '%".$height."%'AND traywidth LIKE '%".$width."%' AND traydepth LIKE
                    '%".$depth."%' AND trayrange LIKE '%".$varRange."%' AND 
                    traytype LIKE '%".$varType."%' AND trayshape LIKE '%".$varShape."%' ";


    $r_query = mysql_query($sql);

    while ($row = mysql_fetch_array($r_query))
            { 
            echo '<br /> Tool Code:   '.$row['toolcode'];
            echo '<br /> Description: '.$row['description']; 
            echo '<br /> Tray range:  '.$row['trayrange']; 
            echo '<br /> Tray type:   '.$row['traytype'];
                echo '<br /> Tray size:   '.$row['traysize']; 
            echo '<br /> Tray shape:  '.$row['trayshape'] . '<br />' . 
                            '<br />'; ;  
                    }

        if (mysql_num_rows($r_query) <= 0){
            echo 'No results match your search, please try again';
               }
        ?>
      </body>

如果有人可以提供帮助,那就太好了,在此先感谢:)

4

3 回答 3

0

您的假设是正确的,如果使用“任何”选项,最好不要在查询中包含过滤器(因为您正在生成查询)。

另一种选择是使用多选控件,如果选择的长度为零,则不要太生成过滤器

需要注意的是,如果执行查询的表增长,LIKE 关键字的广泛使用将导致性能问题。

于 2012-10-09T12:15:16.417 回答
0

您可以使用可选参数,如下所示。您可以将“全部”选项的值设置为星号或类似名称,然后按如下方式编写 SQL 代码:

"SELECT * FROM delyn 
WHERE description LIKE '%".$descrip."%' 
AND ((.$height.   = '*') OR (trayheight LIKE '%".$height."%'))
AND ((.$width.    = '*') OR (traywidth LIKE '%".$width."%')) 
AND ((.$depth.    = '*') OR (traydepth LIKE %".$depth."%')) 
AND ((.$varRange. = '*') OR (trayrange LIKE '%".$varRange."%')) 
AND ((.$varType.  = '*') OR (traytype LIKE '%".$varType."%')) 
AND ((.$varShape. = '*') OR (trayshape LIKE '%".$varShape."%'))";

此示例假定第一个选择 - 描述需要一个值。用户可以将其余选择设置为“全部”。

于 2012-10-09T12:19:22.300 回答
0

你可以用不同的方式构建你的 $sql :

$varRange="";
if ($_POST['trayrange'] != "*") {
$varRange="and trayrange like %".$_POST['trayrange']."%";
}

然后连接

$sql="select"...$varRange...;
于 2012-10-09T12:27:13.693 回答