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 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器