Php 文本中的sprintf和%符号

Php 文本中的sprintf和%符号,php,mysql,printf,mysql-escape-string,Php,Mysql,Printf,Mysql Escape String,我最近遇到的一个问题是,当尝试使用此代码更新数据库中的字段时,将无法工作。我追溯到在更新的文本中有一个%符号($note,然后$note\u转义)。。。不过,用sprintf插入它还是很好的 我应该不使用sprintf进行更新,还是应该采用不同的格式 我做了一些搜索,但什么也找不到 $id = mysql_real_escape_string($id); $note_escaped = mysql_real_escape_string($note); $editedby = mysql_real

我最近遇到的一个问题是,当尝试使用此代码更新数据库中的字段时,将无法工作。我追溯到在更新的文本中有一个
%
符号(
$note
,然后
$note\u转义
)。。。不过,用sprintf插入它还是很好的

我应该不使用
sprintf
进行更新,还是应该采用不同的格式

我做了一些搜索,但什么也找不到

$id = mysql_real_escape_string($id);
$note_escaped = mysql_real_escape_string($note);
$editedby = mysql_real_escape_string($author);
$editdate = mysql_real_escape_string($date);
//insert info from form into database
$query= sprintf("UPDATE notes_$suffix SET note='$note_escaped', editedby='$editedby', editdate='$editdate' WHERE id='$id' LIMIT 1");

多谢

您可以在mysql中用
\%
替换源文本中的
%

您可以在mysql中用
\%
替换源文本中的
%

您使用的sprintf完全错误。删除代码中的函数调用仍会执行相同的操作。应该是:

sprintf("UPDATE notes_%s SET note='%s', editedby='%s', editdate='%s' WHERE id=%d LIMIT 1", $suffix, $note_escaped, $editedby, $editdate, $id);

您应该阅读手册。

您使用的sprintf完全错误。删除代码中的函数调用仍会执行相同的操作。应该是:

sprintf("UPDATE notes_%s SET note='%s', editedby='%s', editdate='%s' WHERE id=%d LIMIT 1", $suffix, $note_escaped, $editedby, $editdate, $id);
您应该阅读手册。

来自:

注意:mysql\u real\u escape\u string()不转义%and\ux。如果与LIKE、GRANT或REVOKE组合,则这些是MySQL中的通配符。

您需要手动用\%和\转义%和u(如果有)。我不建议使用sprintf,只建议改进转义功能。

来源:

注意:mysql\u real\u escape\u string()不转义%and\ux。如果与LIKE、GRANT或REVOKE组合,则这些是MySQL中的通配符。

您需要手动用\%和\转义%和u(如果有)。我不建议使用sprintf,只建议改进转义函数。

sprintf()在PHP中使用不多,除非您需要以某种方式格式化数据。这两条语句在PHP中的工作方式相同:

$num = 42;
$char = 'q';

$text = sprintf('The number is %d and the character is %s', $num, $char);
$text = "The number is $num and the character is $char";
sprintf在C中更多地用于将变量数据“打印”到字符串中。但是PHP已经可以使用双引号字符串来实现这一点,因此,除非您需要使用sprintf的特殊格式化函数(例如,对于2位小数的浮点值,
%0.2f
),否则使用常规字符串方法就更容易了。

sprintf()在PHP中使用不多,除非您需要以某种方式格式化数据。这两条语句在PHP中的工作方式相同:

$num = 42;
$char = 'q';

$text = sprintf('The number is %d and the character is %s', $num, $char);
$text = "The number is $num and the character is $char";

sprintf在C中更多地用于将变量数据“打印”到字符串中。但是PHP已经可以使用双引号字符串来实现这一点,因此,除非您需要使用sprintf的特殊格式化函数(例如,对于2位小数的浮点值,
%0.2f
),否则使用常规字符串方法更容易。

首先,您应该使用它而不是sprintf调用

但如果你必须这样做,你必须使用:

$id = mysql_real_escape_string($id);
$note_escaped = mysql_real_escape_string($note);
$editedby = mysql_real_escape_string($author);
$editdate = mysql_real_escape_string($date);
//insert info from form into database
$query= sprintf("
  UPDATE notes_%s /* this is still open for injection, and cannot be properly escaped with mysql_real_escape_string */
  SET note='%s', 
  editedby='%s', 
  editdate='%s' 
  WHERE id='%d'
  LIMIT 1",
$suffix,
$note_escaped, $editedby, $editdate, $id);

首先,您应该使用而不是sprintf调用

但如果你必须这样做,你必须使用:

$id = mysql_real_escape_string($id);
$note_escaped = mysql_real_escape_string($note);
$editedby = mysql_real_escape_string($author);
$editdate = mysql_real_escape_string($date);
//insert info from form into database
$query= sprintf("
  UPDATE notes_%s /* this is still open for injection, and cannot be properly escaped with mysql_real_escape_string */
  SET note='%s', 
  editedby='%s', 
  editdate='%s' 
  WHERE id='%d'
  LIMIT 1",
$suffix,
$note_escaped, $editedby, $editdate, $id);


mysql中正确的转义方法是\%-mysql中正确的转义方法是\%-删除函数调用?你是说mysql\u real\u escape\u string?这是一个愚蠢的建议give@knittl-否,删除sprintf将给出相同的代码。我到底在哪里提到了mysql\u real\u escape\u string?从你的回答听起来很像,你说的是删除函数调用什么函数调用?-啊,你说的是sprintf,没有在代码中显示escape函数调用。这可能有点误导汉克斯。顺便说一下,我确实读过手册。关于这一点还不是很清楚。。当我试图找出如何将完美工作的INSERT sprintf代码转换为更新代码时,我找到了我的代码,我试图让它工作,但无法…删除函数调用?你是说mysql\u real\u escape\u string?这是一个愚蠢的建议give@knittl-否,删除sprintf将给出相同的代码。我到底在哪里提到了mysql\u real\u escape\u string?从你的回答听起来很像,你说的是删除函数调用什么函数调用?-啊,你说的是sprintf,没有在代码中显示escape函数调用。这可能有点误导汉克斯。顺便说一下,我确实读过手册。关于这一点还不是很清楚。。当我试图找出如何将我完美工作的INSERT sprintf代码转换为更新代码时,我找到了我的代码,我试图让它工作,但失败了。如果你在双引号字符串中使用变量替换创建sql字符串,那么地狱将冻结。只有在你不知道sql注入的情况下,手动构建查询才是危险的,和/或不在乎。在屋顶上大喊大叫手动查询是危险的,这无助于教育初学者。似乎我在某个地方读到,sprintf()是执行mysql查询的一种比普通变量string更快的方法。No。双引号字符串实际上要快得多。没有格式解析开销:如果您在双引号字符串中创建带有变量替换的sql字符串,地狱将冻结。只有在您不知道sql注入和/或不在乎的情况下,手动构建查询才是危险的。在屋顶上大喊大叫手动查询是危险的,这无助于教育初学者。似乎我在某个地方读到,sprintf()是执行mysql查询的一种比普通变量string更快的方法。No。双引号字符串实际上要快得多。没有格式解析开销:谢谢大家:当我接受实际查询并将其粘贴在Sequel Pro中并运行时,是什么真正绊倒了我?它正确地更新了数据库。谢谢大家:当我接受实际查询并将其粘贴在Sequel Pro中并运行时,是什么真正绊倒了我?它正确地更新了数据库。