【问题标题】:PHP for-loop's sql-update not working when using mysqli_query() but echo'd statement goes throughPHP for-loop 的 sql-update 在使用 mysqli_query() 时不起作用,但 echo'd 语句通过
【发布时间】:2019-02-20 12:21:59
【问题描述】:

在一个进程文件中,我正在执行几个 mysqli_query 以将多组信息上传到数据库(不同的表)。第一个 mysqli_query 提交并完美运行,但其余的不起作用。我不确定我的语法哪里有错误,或者 PHP 代码有更多问题,因为我正在使用循环来获取多个更新。在 cmets 中,我添加了代码的用途以及它是否有效

   //at the top declaring the sql queries
   $sql_up_awarder = '';
   $sql_up_history  = '';
   $sql_up_bike  = '';
   $sql_up_social  = '';
   $sql_up_compete  = '';

   //connect to server
   require('conn.php');
   //sqlprevent 
   require('sqlprevent');

  //main table SQL statement that works
  $sql_update = sprintf("UPDATE influencers SET inf_name = %s,inf_email = %s,inf_dob = %s,inf_mobile = %s,inf_location = %s WHERE inf_id = $vid;",
      escapestring($vconncvnl, $vName, 'text'),
      escapestring($vconncvnl, $vEmail, 'text'),
      escapestring($vconncvnl, $vdob, 'text'),
      escapestring($vconncvnl, $vphone, 'text'),
      escapestring($vconncvnl, $vlocation, 'text')
   );
   $result_insert = mysqli_query($vconncvnl, $sql_update);

   if ($vracehistory != '') {
      for($i = 0; $i <count($history); $i++){
         $historyid = $history[$i];
         $valuehistory = $vracehistory[$i];
         $valueresults = $results[$i];
         $sql_up_history .= sprintf("UPDATE inf_race_history SET race_history =%s,results = %s WHERE inf_id = " . "'". $vid . "'" . " AND race_id = '$historyid';". "<br>", escapestring($vconncvnl, $valuehistory,'text'),escapestring($vconncvnl, $valueresults ,'text'));
       }
    } else{
    $vracehistory = '';
    }
    $result_history = mysqli_query($vconncvnl, $sql_up_history);

当我回显$sql_up_history时,它给了我

UPDATE inf_race_history SET race_history ='51',results = '61' WHERE inf_id = 2 AND race_id = 2;
UPDATE inf_race_history SET race_history ='71',results = '81' WHERE inf_id = 2 AND race_id = 3;

当输入到 PHPMyAdmin 结果和工作时,然后按原样更新表格

但是,当我回显 $results_history 时,我什么也得不到 当我尝试使用Mysqli_error($vconncvnl); 来看看它给了我什么时

您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以获取在 ' 附近使用的正确语法 UPDATE inf_race_history SET race_history ='71',results = '81' WHERE inf_id =' at line 1

我已尝试更改更新语句中的值,使其值像

Update 'inf_race_history'

在`标记中但没有任何区别


编辑

根据 davids 的建议,我从更新部分删除了 &lt;br&gt;,然后将 mysqli_error() 更改为此

您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 'UPDATE inf_race_history SET race_history ='71',results = '81' WHERE inf_id = 2 A' 附近使用正确的语法

【问题讨论】:

  • 您在 SQL 语句中添加了 &lt;br&gt;。那不是 SQL 代码,那是 HTML 代码。这似乎只是一个错字。
  • mysqli_query() 无法运行多个查询 - 使用 mysqli_multi_query()
  • 还有 sprintf() 和你的 escapestring() 我很确定他们不会完全阻止所有 SQL 注入,请使用准备好的语句。
  • No no @NigelRen mysqli_multi_query() is a very bad建议这意味着像1'; DELETE TABLE table这样的SQL注入也是可能的..除了你不想保护带有不安全 mysqli_real_escapse_string() 函数的 SQL 注入,如果使用不当,可能会造成更大的伤害和好处..
  • 为什么不直接运行更新查询,而不是构建一个长而无效的命令?

标签: php mysql sql loops


【解决方案1】:

使用 mysqli_multi_query() 代替 mysqli_query()。 建议用于多个查询,执行一个或多个用分号连接的查询。

【讨论】:

  • 如果我使用不同的表,这是否有效?只要他们共享相同的$sql_update .= ?
  • 即使你有不同的表也不起作用。使用 mysqli_query() 执行以分号分隔的多个查询时存在问题。只需将最后一行替换为 mysqli_multi_query
  • 我只有 1 个问题,尽管 @Rizwan Khan,Raymond Nijland 在问题 cmets 中说“No no NigelRen mysqli_multi_query() 是一个非常糟糕的建议,这意味着 SQL 注入像 1'; DELETE TABLE 表也是可能的。此外,您不想使用不安全的 mysqli_real_escapse_string() 函数来保护 SQL 注入,该函数在使用错误时会造成更多的伤害和好处。”这将如何影响代码呢?有点担心
  • 有两种方法可以保护 SQL 注入 - 一种方法是进行输入数据过滤。另一种方法是在 PHP 中使用 PDO,请使用 PDO 类,这是最好的方法,因为它为您提供了强大的构造,如准备好的语句。转过php.net/manual/en/class.pdo.php就很好用了。
  • 即使我是唯一一个这样做的人和它的后端 cms 系统,它是否也非常必要? @Rizwan Khan
猜你喜欢
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 2013-05-03
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多