【问题标题】:How to prevent users to search null/space value because it keeps returning all values from database如何防止用户搜索空值/空格值,因为它不断返回数据库中的所有值
【发布时间】:2020-08-28 04:36:08
【问题描述】:

当我搜索空值或空格值时,数据库中的所有结果都会出现在我的 html 表中。 我想阻止用户搜索空值或空格值。

我尝试了这篇文章,但现在帮助了我,How to prevent a database search from running on an empty string?

这是后端脚本-

<?php

//fetch.php

$connect = new PDO("mysql:host=localhost;dbname=searchv3", "root", "");

$output = '';

$query = '';

$data = [];

if(isset($_POST["query"]))
{
 $search = str_replace(",", "|", $_POST["query"]);
 $query = "
 SELECT * FROM number_list
 WHERE IMSI REGEXP '".$search."' 
 OR Mobile_no REGEXP '".$search."' 
 OR Backup_date REGEXP '".$search."' 
 OR Sr REGEXP '".$search."' 
 

 ";
}
else
{
 $query = "
 SELECT * FROM number_list order by Sr DESC LIMIT 50;
 ";
}

$statement = $connect->prepare($query);
$statement->execute();

while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
 $data[] = $row;
 
}

echo json_encode($data);
$connect = null;

?>

我也会分享前端jav脚本-

<script>
$(document).ready(function(){
 load_data();
function load_data(query)
 {
  $.ajax({
   url:"fetch_numberlist.php",
   method:"POST",
   data:{query:query},
   dataType:"json",
   success:function(data)
   {
    $('#total_records').text(data.length);
    var html = '';
    if(data.length > 0)
        
    {
     for(var count = 0; count < data.length; count++)
     {
      html += '<tr>';
      html += '<td>'+data[count].Sr+'</td>';
      html += '<td>'+data[count].IMSI+'</td>';
      html += '<td>'+data[count].Mobile_no+'</td>';
      html += '<td>'+data[count].Backup_date+'</td>';
     }
    }
    else
    {
            html = '<tr><td colspan="4">No Data Found</td></tr>';
    }
    $('tbody').html(html);
   }
  })
 }

 $('#search').click(function(){
  var query = $('#tags').val();
  load_data(query);
 });
 })
</script>

【问题讨论】:

  • 检查 PHP 代码中的 NULL/空格,并仅在检查结果为 false 时将条件表达式连接到查询文本。
  • 我建议你在客户端和服务器端都这样做。在客户端中,您必须在发出服务器请求之前检查查询字符串是否有效。这样您将避免不必要的请求(更好的带宽,降低成本,...)此外,由于客户端代码可以被操纵,您还必须在服务器端进行。
  • 警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!

标签: javascript php mysql ajax


【解决方案1】:
...

$data = [];

// `??` operator returns value if isset needed post element, else use empty string
// trim - remove start and ending spaces from string. 
//      So it return empty string if user input only space
$post_query = trim($_POST["query"] ?? '');

if($post_query)
{
    $search = str_replace(",", "|", $post_query);

...

【讨论】:

  • 你忘了在你的 js 代码循环中关闭 标签for(var count = 0; count &lt; data.length; count++)
  • 您好,感谢您的回复和代码。但是我尝试输入您的代码并尝试不同的方式,它们现在结果为空,我无法搜索任何内容。帮助我。
  • 您是否也将$search = str_replace(",", "|", $_POST["query"]); 更改为$search = str_replace(",", "|", $post_query);
  • 是的,我试过改变,$search = str_replace(",", "|", $post_query);刚刚,还不行。
  • 现在,它可以工作了,谢谢,我会将其标记为答案。声明 php 变量时,我们只是缺少半列。 :( 非常感谢。
猜你喜欢
  • 2011-11-04
  • 2017-09-21
  • 1970-01-01
  • 2017-04-03
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 1970-01-01
相关资源
最近更新 更多