我一直在阅读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标签。)

不幸的是,截至目前,我仍在逃脱后显示出新的字符。我想念什么?

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

不幸的是,事实并非如此。我仍然有newline字符

有帮助吗?

解决方案

由于您使用了准备好的语句,因此您也不应该逃脱弦。

字符串逃逸是为了将值嵌入SQL查询字符串本身时,但是通过使用准备的语句,您完全正确地避免了这样做。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top