转义和 HTML 特殊字符

Escaping and HTML special characters

我在数据库插入之前转义数据使用:$entry = mysqli_real_escape_string($link, $value); 然后在显示之前在输出上使用 htmlspecialchars(),但是在我的输出中我似乎在字符串中有斜杠,如 It\'s not working。显然我不想这样。

代码(为简单起见缩写):

function insertData($post)
    $dbc = mysqli_connect(DB_HOST, DB_UN, DB_PW, DB_NAME);
    foreach ($post as $key => $value) {
        $post[$key] = mysqli_real_escape_string($dbc, $value);
    }
    $insert = 'INSERT INTO products_test ('.array_keys($entry)[0].','.array_keys($entry)[1].') VALUES ("'.array_values($entry)[0].'","'.array_values($entry)[1].'")';

    if (mysqli_query($dbc, $insert)) {
        echo htmlspecialchars($post['name']).' has been added to the inventory';
    }

$post = [
    'name' => $_POST['name'],
    'narrative' => $_POST['narrative']
];

insertData($post);

echo stripslashes("It\'s not working");

示例:https://3v4l.org/tmqNP

手动:http://php.net/manual/en/function.stripslashes.php

这将去除 mysqli_real_escape_string 中的斜杠,但不会回显转义字符串,而是回显原始字符串。

if (mysqli_query($dbc, $insert)) {
    echo htmlspecialchars($post['name']).' has been added to the inventory';
}

注意:OP 在我发布后更改了有问题的代码以反映我的回答。我不只是逐字复制他的代码。