2009-08-08 87 views
0

我一直在對mysqli_real_escape_string()進行一些閱讀,並且在我的內容正確轉義後,我在再次拔出時遇到了一些麻煩。如何在mysqli_real_escape_string和 nl之間切換?

這裏是我的代碼:

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標籤。)

不幸的是,截至目前,我仍然在轉義後顯示換行符。我錯過了什麼?

非常感謝您的時間,並提供任何意見非常感謝。

不幸的是,情況並非如此。我仍然收到換行符

回答

2

由於您使用的是準備好的語句,因此您不應該也逃避字符串。

字符串轉義是爲了將值嵌入到SQL查詢字符串本身中,但通過使用預處理語句,您完全可以避免這樣做。

+0

非常感謝,我沒有意識到這一點! – 2009-08-08 21:41:12