【问题标题】:Use Ajax parameter in php fetch will not work在 php fetch 中使用 Ajax 参数将不起作用
【发布时间】:2020-02-10 11:51:44
【问题描述】:

这是我的代码: Ajax/HTML:

      <select id='overallval'>
       <option value='81'>81</option>
       <option value='83'>83</option>
       <option value='88'>88</option>
     </select>

      $('#overallval').change(function(){
    table.draw();
  });

$(document).ready(function() 
{
    var table = $('#example').DataTable( 
    {
        "processing": true,
        "serverSide": true,
        "type": "POST",
        "ajax": {
            "url": 'https://WEBSERVER.com/server_processing.php',
            'data': function(data){
              // Read values
              var overall = $('#overallval').val();
              // Append to data
              data.overallpara = overall;
            }
        },
        "columnDefs": [...]

我的 PHP:

$where = "overall > '.$_POST['overallpara'].'";
echo json_encode(SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns,null, $where ));

如果我只是使用例如

$where = "overall > 88";

一切正常.. 但是我上面发布的使用 POST-var 的方法没有获取任何数据。 我使用 Firefox 来调试由 htm 文件发送的参数,并发送一个值为“88”的“overallpara”。

那么可能是什么问题?

【问题讨论】:

  • $where = "overall &gt; {$_POST['overallpara']}";
  • $where = "overall &gt; '".$_POST['overallpara']."'";
  • Nicks 解决方案给了我一个“ataTables 警告:表 id=example - 发生 SQL 错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;请检查与您的 MariaDB 服务器版本相对应的手册,以便正确使用语法”错误
  • Barmar 的那个只给了我所有的记录(总体上也低于你可以选择的任何值)
  • 看起来你正在创建一个 sql 注入漏洞,你需要以某种方式清理你的值,所以也许 intval() 就是你需要的。 $where = "overall &gt; ".intval($_POST['overallpara']);

标签: php sql ajax fetch


【解决方案1】:

你的PHP代码错误$where = "overall &gt; '.$_POST['overallpara'].'";

//Replace it with my code
$where = "overall > {$_POST['overallpara']}";

【讨论】:

  • 谢谢,但这给了我以下异常:DataTables warning: table id=example - An SQL error occurred: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY playerid ASC LIMIT 0, 10' at line 4
【解决方案2】:

我不得不在我的 php 脚本中使用 $_REQUEST 而不是 $_POST。现在它起作用了。谁能解释我为什么需要它?这只是一个测试,所以我知道为什么我必须使用它。 :(

【讨论】:

    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 2017-09-29
    • 2012-04-12
    • 2020-10-20
    • 2019-02-25
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    相关资源
    最近更新 更多