dongzhi2332 2012-10-09 09:44
浏览 26
已采纳

如何阻止空SQL查询显示所有结果?

I am creating a meat packaging search form, users can search for different packages using multiple forms and dropdown boxes. I have a had a lot of problems but most of it is sorted now, I only need to create an "Any" search for the dropdown boxes and the problem of empty textboxes displaying all results.

Currently when a user sends a search, they may have entered in some other text boxes, but when one of the forms is left empty that automatically displays all of the results. I want it so when a search is sent and a box is empty, the code ignores that form and just checks the ones that have info inside of them.

here is my test code (not my current final form code):

<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']);

$sql = "SELECT * FROM delyn WHERE description LIKE '%" . $descrip . "%'";

$r_query = mysql_query($sql);

if ($descrip === "")
{
    echo 'Null value';
}

while ($row = mysql_fetch_array($r_query))
{
    echo '<br /> Description: ' . $row['description'];
}

?>
</body>

Anyone have any ideas on how to stop this?

EDIT: Sorry here is my HTML with the search boxes. The above php is just where the values are sent.

<body>
    <form action="form5null.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="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="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 &amp; 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="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>
  • 写回答

2条回答 默认 最新

  • duan6832168 2012-10-09 13:53
    关注

    Add the following as the first option to all your dropdowns:

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

    Then use the SQL I provided in your duplicate question here:

    Make SQL ignore a specific search value if clicked in a dropdown?

    Dirk Nachbar gave a good answer as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?