0

抱歉,这个问题相当模棱两可。

我正在为包装数据库创建一个搜索表单,在过去的几天里,我在创建一个搜索数据包高度的表单时遇到了一些问题。我希望表单显示与用户输入相关的所有结果,而不是只返回确切的结果。我无法得到这样的工作。所以我求助于创建一个在一个范围内搜索的下拉框。

HTML 下拉菜单:

        <body>
               <label for="trayheight">Height: </label>
                <select name="trayheight">
                    <option value="">All</option>
                    <option value="1">100 - 199 range</option>
                    <option value="2">200 - 299 range</option>
                    <option value="3">300 - 399 range</option>
                </select><br />
        </body>

这是下拉范围(我还将添加一个搜索 70 - 80 个数据包的范围)。正如您所看到的,每个选项值仅包含一个数字(“1,2,3”),因此数据库将返回任何包含(例如)1 的结果。因此,如果用户搜索“100-199”范围内的内容,SQL 查询将返回该范围内的所有结果,但目前还返回任何带有“1”的内容,例如高度“201”。我想更改它,以便 SQL 查询将获取该值,然后将其与记录中的第一个数字进行比较,例如 (1)21 中的 1,因此返回的结果应该只在该范围内,没有别的例如“212”不会返回。是否有某种形式的服务器代码可以处理这个问题?

这是我的服务器端代码:

       <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']); 
            $width   = mysql_real_escape_string($_POST['width']);
            $depth   = mysql_real_escape_string($_POST['depth']);

            $varHeight= mysql_real_escape_string($_POST['trayheight']);
            $varRange = mysql_real_escape_string($_POST['trayrange']);
            $varType  = mysql_real_escape_string($_POST['traytype']);
            $varShape = mysql_real_escape_string($_POST['trayshape']);
            $varImage = mysql_real_escape_string($_POST['imagename']);

            $sql = "SELECT * FROM delyn WHERE 
                        description LIKE '%".$descrip."%'  
                    AND trayheight  LIKE '%".$varHeight."%'
                    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 /> <img src="   '. $row['imagename'] . '" width="180" length="100">';
                        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>

Thanks guys.

4

1 回答 1

0

Just construct the range:

if (isset($varHeight) && !empty($varHeight)) {
    $low = intval($varHeight."00");
    $high= intval($varHeight."99");
} else {
   $low = intval("000");
   $high= intval("999");
}

and change the sql query to search for this range:

AND trayheight  LIKE '%".$varHeight."%'

to

"AND trayheight  BETWEEN '".$low."' AND '".$high."'"

Of course proper validation should be placed to ensure that $varHeight is a number (presumably between 1 and 9)

UPDATED in order to catch the absence of $varHeight filter. This assumes that the total minimum of all possible values for $varHeight is 000 and the total maximum is 999

于 2012-10-12T08:09:09.207 回答