提交空搜索字段时如何停止搜索字段返回所有结果?

发布于 2024-12-11 23:31:42 字数 1431 浏览 0 评论 0原文

如果我单击搜索字段并提交而不输入任何文本,则会返回数据库中的所有数据。这是如何停止的而不发生任何事情?

查看网站:

weezy.co.uk/newresults.php

谢谢!

詹姆斯

 <?php

    $conn = mysql_connect("cust-mysql-123-02", "uwee_641290_0001", "La0%-Mr4");

    if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
    }

    {

      $search = "%" . $_POST["search"] . "%";
    $searchterm = "%" . $_POST["searchterm"] . "%";

    }

    if (!mysql_select_db("weezycouk_641290_db1")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
    }

    $sql = "SELECT name,lastname,email 
    FROM   test_mysql
    WHERE  name LIKE '%".$search."%' AND lastname LIKE '%".$searchterm."%'";

    $result = mysql_query($sql);

    if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
    }

    if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
    }


while ($row = mysql_fetch_assoc($result)) {
    echo '<br>';
    echo '<br>';
    echo '<div class="data1">';
    echo $row["name"];
    echo '</div>';
    echo '<br>';
    echo '<div class="data2">';
    echo $row["lastname"];
    echo '</div>';
    echo '<br>';
    echo '<div class="data3">';
    echo $row["email"];
    echo '</div>';
    }

    mysql_free_result($result);

    ?>

If i click on my search field and submit it without entering any text all the data in my database is returned. How is this stopped so nothing happens?

Check out the site:

weezy.co.uk/newresults.php

Thanks!

James

 <?php

    $conn = mysql_connect("cust-mysql-123-02", "uwee_641290_0001", "La0%-Mr4");

    if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
    }

    {

      $search = "%" . $_POST["search"] . "%";
    $searchterm = "%" . $_POST["searchterm"] . "%";

    }

    if (!mysql_select_db("weezycouk_641290_db1")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
    }

    $sql = "SELECT name,lastname,email 
    FROM   test_mysql
    WHERE  name LIKE '%".$search."%' AND lastname LIKE '%".$searchterm."%'";

    $result = mysql_query($sql);

    if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
    }

    if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
    }


while ($row = mysql_fetch_assoc($result)) {
    echo '<br>';
    echo '<br>';
    echo '<div class="data1">';
    echo $row["name"];
    echo '</div>';
    echo '<br>';
    echo '<div class="data2">';
    echo $row["lastname"];
    echo '</div>';
    echo '<br>';
    echo '<div class="data3">';
    echo $row["email"];
    echo '</div>';
    }

    mysql_free_result($result);

    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

原野 2024-12-18 23:31:42

您应该在进行查询之前检查它是否为空:

if(empty($_POST['searchterm'])){
    //don't search and show an error message
}else{
   //proceed as normal, do the query
}

否则您可能最终会进行如下查询:

WHERE name LIKE('%%')

这非常昂贵并返回所有数据库行

you should check if it's empty before making a query:

if(empty($_POST['searchterm'])){
    //don't search and show an error message
}else{
   //proceed as normal, do the query
}

otherwise you might end up making a query like:

WHERE name LIKE('%%')

which is really expensive and returns all your database rows

染火枫林 2024-12-18 23:31:42

执行此操作的最佳方法(imo)是使用简单的 JavaScript 检查输入是否为空。

Best way to do this (imo) is to have a simple javascript checking if the input is blank or not.

撧情箌佬 2024-12-18 23:31:42

使用 javascript/Jquery 做一些前端总是明智的。表单验证,提示用户输入一些内容。

完成后,您还可以使用以下命令检查后端:

if(isset($_POST['searchterm'])){
    // search for the results.
}else{
   // do nothing or show proper message
}

It is always wise to do some front end using javascript/Jquery. form validation where you are prompting users to input something.

Once you are done you may also check on the back end using the following:

if(isset($_POST['searchterm'])){
    // search for the results.
}else{
   // do nothing or show proper message
}
<逆流佳人身旁 2024-12-18 23:31:42

我认为最好的方法是在搜索框为空时禁用客户端的提交按钮。

您可以执行以下操作:

$(document).ready(function()
{
   $('#searchBox').keyup(function() 
   {
      if($(this).val() == '')
      {
        $('#searchButton').attr('disabled', true);
      }
      else
      {
        $('#searchButton').removeAttr('disabled');
      }

   });
});

您的 html 如下:

<input type='text' id="searchBox" />
<input type='button' id='searchButton' value='search' disabled/>

确保按照 Nicola 的指示在服务器端进行验证。

I think the best way would be to disable the submit button on the client side whenever your search box is empty.

You could do something like:

$(document).ready(function()
{
   $('#searchBox').keyup(function() 
   {
      if($(this).val() == '')
      {
        $('#searchButton').attr('disabled', true);
      }
      else
      {
        $('#searchButton').removeAttr('disabled');
      }

   });
});

where your html is like:

<input type='text' id="searchBox" />
<input type='button' id='searchButton' value='search' disabled/>

Make sure to validate on the server side as Nicola has indicated.

国粹 2024-12-18 23:31:42

我在上面的代码中遇到了一些问题。因此,上述代码的以下改进版本可以正常工作:

<form action="searchengine.php" method="POST">
<input type="text" id = "searchbox" name="searchterm" placeholder="Search here">
<input type="submit" id = "searchbutton" value="Search" style="display:none;">
</center>
<script src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#searchbox').keyup(function()
{
if($(this).val() == '')
{
$('#searchbutton').hide();
}
else
{
$('#searchbutton').show();
}

});
});

</script>

I was facing some problems in the above code. So the following improved version of the above code works just fine:

<form action="searchengine.php" method="POST">
<input type="text" id = "searchbox" name="searchterm" placeholder="Search here">
<input type="submit" id = "searchbutton" value="Search" style="display:none;">
</center>
<script src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#searchbox').keyup(function()
{
if($(this).val() == '')
{
$('#searchbutton').hide();
}
else
{
$('#searchbutton').show();
}

});
});

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文