PHP 中的转义引号

发布于 2024-10-25 08:50:03 字数 293 浏览 5 评论 0原文

尝试查询 MySQL 数据库时如何转义 PHP 中的引号?

无需在每个值上添加 addslashes

$fname = addslashes("Value's with quote''s'");
$lname = addslashes("Value's with quote''s'");

How do I escape quotes in PHP when trying to query a MySQL database?

Without adding addslashes on every value:

$fname = addslashes("Value's with quote''s'");
$lname = addslashes("Value's with quote''s'");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情归归情 2024-11-01 08:50:03

正确的方法是使用准备好的语句,例如通过 PDO

如果您做不到这一点,则必须处理使用 mysql_real_escape_string() 传递到数据库查询的所有值 - 不,只需在所有上执行此操作>$_POST 数据不是一个选项,因为这会使它们无法用于 HTML 输出等。您可以创建一个 $_ESC 或类似的东西...但请注意,此变量将不是超全球化的!

The proper way is using prepared statements, e.g. via PDO.

If you can't do that, you have to process all values which are passed into a database query with mysql_real_escape_string() - and no, doing that simply on all $_POST data is not an option since that would render them unusable for HTML output, etc. You could create a $_ESC or something similar though... but note that this variable will not be superglobal!

雄赳赳气昂昂 2024-11-01 08:50:03

您应该在每个字符串值上转义特殊字符(不仅是引号)(转义您不打算在查询中用引号括起来的值是没有用的。这些值需要另一种治疗)。

为了避免无聊的重复输入,您可以对循环中的数组项应用转义函数。

如果您使用 MySQL 并进行 INSERT/UPDATE 查询,则可以使用此辅助函数:

function dbSet($fields) {
  $set = '';
  foreach ($fields as $field) {
    if (isset($_POST[$field])) {
      $set .= "`$field`='" . mysql_real_escape_string($_POST[$field]) . "', ";
    }
  }
  return substr($set, 0, -2); 
}

它的使用方式如下:

$id     = intval($_POST['id']);
$table  = 'users';
$fields = explode(" ","name surname lastname address zip fax phone");
$query  = "UPDATE `$table` SET ".dbSet($fields).", `date`=NOW() WHERE id=$id";

另外,不要忘记根据需要使用 mysql_set_charset() 设置正确的编码对于 mysql_real_escape_string() 函数。

You ought to escape special characters (not only quotes) on every string value (it's useless to escape values you're not going to enclose in quotes in a query. Those values require another treatment).

To avoid boring repetitive typing you can apply an escaping function to array items in a loop.

In case you're using MySQL and for INSERT/UPDATE queries, you can use this helper function:

function dbSet($fields) {
  $set = '';
  foreach ($fields as $field) {
    if (isset($_POST[$field])) {
      $set .= "`$field`='" . mysql_real_escape_string($_POST[$field]) . "', ";
    }
  }
  return substr($set, 0, -2); 
}

It is used like this:

$id     = intval($_POST['id']);
$table  = 'users';
$fields = explode(" ","name surname lastname address zip fax phone");
$query  = "UPDATE `$table` SET ".dbSet($fields).", `date`=NOW() WHERE id=$id";

Also don't forget to set proper encoding using mysql_set_charset() as it's required for the mysql_real_escape_string() function.

贩梦商人 2024-11-01 08:50:03

一个好主意是使用 PDO 准备好的语句,如此处所述。

它会自动转义这些字符。

A good idea would be using PDO prepared statements as described here.

It will automatically escape those characters.

世界等同你 2024-11-01 08:50:03

首先,不要使用 addslashes() - 不建议将其与转义数据库查询字符串一起使用,因为它不会转义实际需要转义的所有内容;有一些角色仍然可以通过。

正确的解决方案取决于您使用的数据库。假设您使用的是 MySQL,则代替 addslashes() 使用的正确函数是 mysql_real_escape_string()

您可能会注意到,在每一行上使用它比 addslashes() 更冗长,因此它并不能真正回答您的问题。

如果您的字段都是单独的变量(按照您的示例),那么您确实不得不为一堆代码行执行此操作。

如果您使用的是数组(例如 $_POST),那么您可以在循环中执行此操作,这将使事情变得更加整洁 - 您可以执行以下操作

foreach($_POST as $key=>$value) {
    $sqlstrings[$key]="`".$key"` = '".mysql_real_escape_string($value)."'";
}
$sql = "update table ".implode(' ',$sqlstrings)." where id=".$update_id;

:执行 SQL 的日期方法是使用对象模型而不是手动构建查询。 PHP 有许多可能有帮助的库:mysqli 是一个改进的 MySQL 库,PDO 是一个与数据库无关的库。与直接构建 SQL 代码相比,其中任何一个都会为您提供更好的安全性和灵活性。但是,如果您已经有大量代码,那么它们将代表相当大的代码更改开销,因此您可能希望在短期内使用上面讨论的 mysql_real_escape_string() 选项。不过,我确实建议对它们进行投资。

Firstly, don't use addslashes() - it is not recommended for use with escaping DB query strings because it doesn't escape everything that actually needs to be escaped; there are some characters that can still get through.

The correct solution depends on the database you're using. Assuming you're using MySQL, the correct function to use instead of addslashes() is mysql_real_escape_string().

You probably notice that using this on every line is even more verbose than addslashes(), so it doesn't really answer your question.

If your fields are all separate variables (as per your example), then you're really stuck with doing that for a bunch of lines of code.

If you're using an array (eg $_POST), then you can do it in a loop, which will make things a lot neater - you can do things like this:

foreach($_POST as $key=>$value) {
    $sqlstrings[$key]="`".$key"` = '".mysql_real_escape_string($value)."'";
}
$sql = "update table ".implode(' ',$sqlstrings)." where id=".$update_id;

A more up-to-date method for doing SQL is to use an object model rather than manually building the queries. PHP has a number of libraries that may help: mysqli is an improved MySQL library, and PDO is a database-neutral library. Either of these would give you much better security and flexibility than building the SQL code directly. However if you already have a lot of code in place then they would represent a fairly significant overhead of code changes, so you may want to go with the mysql_real_escape_string() option discussed above in the short term. I do recommend investating them them though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文