php - PHP 中的转义字符和保留

标签 php mysql insert escaping mysql-real-escape-string

我有一个 PHP 应用程序,我从多个来源获取名字和姓氏(API、文件等) 假设我得到像 john', john" 这样的名称,如何将名称保存在数据库中,'," 可以在任何地方,问题是当我编写像

这样的查询时
Insert into table values('".$a."');

因此,如果 $a 包含名称 John',则查询将变为 Insert into table Values('John'');,这是错误的,(末尾为 2 ') 如何用 PHP 来满足这些场景?

最佳答案

$string=mysqli_real_escape_string($connection,$string);

This function is used to create a legal SQL string that you can use in an SQL statement. The given string is encoded to an escaped SQL string, taking into account the current character set of the connection.

<强> Reference

关于php - PHP 中的转义字符和保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19544803/

相关文章:

php - Elastica添加文档将覆盖现有文档

php - 如何使用 PHP 将 SQL Select 语句链接到 HTML 按钮

mysql performance INSERT into table SELECT 报告

mysql - 如何将两条 SQL 语句合并为一条语句?

javascript - 获取 HTML 标签值并分配给 PHP 变量

php - 对mysql中的多个字段进行加权搜索的最佳方法?

php - 不能将 PDOException 类型的对象用作数组

php - 用新外键替换列值的方法?

sql - 在 MySQL 的连接中优化 OR

php - 有没有办法检查 php mysql 中 "SELECT... INTO OUTFILE"的执行时间(已过或剩余)?