首页 > 解决方案 > 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 变量时,它会给出如下输出:

我们不必自己转义 \' 字符不是很好吗?

但是当我看到数据库字段时,我发现了这个:

我们不用自己转义 ' 字符不是很好吗?

标签: phpmysqlmysql-real-escape-string

解决方案


推荐阅读