我怎样才能在mysqli_real_escape_string和\ nl之间交替?

时间:2009-08-08 20:21:41

标签: mysqli newline mysql-real-escape-string

我一直在读一些关于mysqli_real_escape_string()的内容,并且在我的内容被正确转义之后,当我再次将它拉出来时,我遇到了一些问题。

这是我的代码:

function update_section_content() {
    $name = mysqli_real_escape_string($this->conn, $_POST['name']);
    $text = mysqli_real_escape_string($this->conn, $_POST['content']);

    // First, we do an update
    $update_query = "UPDATE sections SET content = ? WHERE name = ?";
    if($update_stmt = $this->conn->prepare($update_query)) {
        $update_stmt->bind_param('ss', $text, $name);
        $update_stmt->execute();
        // If the update was successful, read in what we just updated
        if($update_stmt->affected_rows == 1) {
            $read_query = "SELECT content FROM sections WHERE name = ?";
            if($read_stmt = $this->conn->prepare($read_query)) {
                $read_stmt->bind_param('s', $name);
                    $read_stmt->execute();
                $read_stmt->bind_result($content);
                if($read_stmt->fetch()) {
                    echo nl2br($content);
                }
            }
        }
        $read_stmt->close();
        $update_stmt->close();
}

我对以下代码的希望是它会更新记录并转义任何不良字符,然后,成功后,在保持其先前的视觉完整性的同时读回更新的查询。 (也就是说,我希望textarea能够回显这些内容以显示换行符而不是br标签。)

不幸的是,到目前为止,我仍然在转义后显示换行符。我错过了什么?

非常感谢您的时间,非常感谢您提供的任何建议。

不幸的是,事实并非如此。我仍然得到换行符

1 个答案:

答案 0 :(得分:2)

由于你正在使用预准备语句,所以你也不应该逃避你的字符串。

字符串转义适用于将值嵌入SQL查询字符串本身时,但通过使用预准备语句,您可以正确地避免这样做。

相关问题