php - mysqli_real_escape_string() 在数据库中不起作用
问题描述
我正在使用mysqli_real_escape_string()
防止 SQl 注入的功能。我的代码
<?php
// open a connection
$dhost = 'localhost';
$duser = 'user';
$dpw = 'pass';
$dname = 'db_name';
$connection = mysqli_connect($dhost, $duser, $dpw, $dname);
// test the connection
if(mysqli_connect_errno()){
die('Something went wrong with the database<br><br> '
. mysqli_connect_error() . ':'
. mysqli_connect_errno());
}
$query = "Isn't it nice that we don't have to escape ' characters all by ourselves?";
echo $query.'<br>';
$escaped = mysqli_real_escape_string($connection , $query);
echo $escaped.'<br>';
mysqli_query($connection,"INSERT into emp (name) VALUES ('$escaped')");
mysqli_close($connection);
?>
当我打印 $escaped 变量时,它会给出如下输出:
我们不必自己转义 \' 字符不是很好吗?
但是当我看到数据库字段时,我发现了这个:
我们不用自己转义 ' 字符不是很好吗?
解决方案
推荐阅读
- ruby-on-rails - 将参数添加到选项卡以在 if 语句中使用
- arduino - 将字符串转换为 Char* 并使用 SHA-256 对其进行哈希处理
- codenameone - 在 Codenameone 中控制宽度渲染动画
- java - EditText 数字分组仅显示 4 个字符
- javascript - 我如何将 2 个提示答案转换为 1 个结果以进行连接
- ios - 您是否需要提供每个图标尺寸才能将您的应用发布到应用商店?iOS
- python - 如何在python中创建一个链表
- python - 使用 write() 方法合并两个文本文件时删除了行
- c# - A way to add int value to textbox with a button click and then subtract from the int value with another button
- python - 从蒙特卡洛总结场景