提交空搜索字段时如何停止搜索字段返回所有结果?
如果我单击搜索字段并提交而不输入任何文本,则会返回数据库中的所有数据。这是如何停止的而不发生任何事情?
查看网站:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该在进行查询之前检查它是否为空:
否则您可能最终会进行如下查询:
这非常昂贵并返回所有数据库行
you should check if it's empty before making a query:
otherwise you might end up making a query like:
which is really expensive and returns all your database rows
执行此操作的最佳方法(imo)是使用简单的 JavaScript 检查输入是否为空。
Best way to do this (imo) is to have a simple javascript checking if the input is blank or not.
使用
javascript/Jquery
做一些前端
总是明智的。表单验证,提示用户输入一些内容。完成后,您还可以使用以下命令检查后端:
It is always wise to do some
front end
usingjavascript/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:
我认为最好的方法是在搜索框为空时禁用客户端的提交按钮。
您可以执行以下操作:
您的 html 如下:
确保按照 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:
where your html is like:
Make sure to validate on the server side as Nicola has indicated.
我在上面的代码中遇到了一些问题。因此,上述代码的以下改进版本可以正常工作:
I was facing some problems in the above code. So the following improved version of the above code works just fine: