2016-11-22 97 views
-1

我試圖寫入數據庫使用CKEditor ..當我按提交它死亡,並說本地主機當前無法處理此請求。 HTTP錯誤500從CKEDITOR寫入數據庫

我只想將textarea保存到數據庫中的一行中,以便我可以將該行讀到另一頁上。

<!DOCTYPE html> 
<html> 

<head> 
    <meta charset="utf-8"> 
    <meta name="robots" content="noindex, nofollow"> 
    <title>Classic editor replacing a textarea</title> 
    <script src="http://cdn.ckeditor.com/4.6.0/standard-all/ckeditor.js"></script> 
</head> 

<body> 
    <form id="editor1" action="save.php" method="post" > 

    <textarea cols="80" id="editor1" name="editor1" rows="10"> 
    </textarea> 

     <p> 
      <input type="submit" value="Submit"> 
     </p> 
    </form> 

    <script> 
     CKEDITOR.replace('editor1'); 
    </script> 
</body> 

</html> 

PHP腳本

<?php 

if(isset($_POST['submit'])) 
{ 
    // Putting data from form into variables to be manipulated 
    $text = $_POST['editor1']; 

    $conn = mysql_connect("localhost","root","root") or die ("Can't connect"); 
    mysql_select_db("managerMessage",$conn); 

    // Getting the form variables and then placing their values into the MySQL table 
    mysql_query("INSERT INTO text (textarea) VALUES ("mysql_real_escape_string($text)"); 
} 
?> 
+0

**停止**使用不推薦使用的'mysql_ *'API。使用'mysqli_ *'或'PDO'與準備好的語句 – Jens

+0

是否獲得了$ text變量中的ckeditor值? – 2016-11-22 11:08:38

+0

每次使用[mysql_'](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) 數據庫擴展在新代碼 ** [一隻小貓被勒死在世界某個地方](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)**它被棄用,並且已經存在了很多年,並且已經消失了永遠在PHP7中。 如果您只是學習PHP,請花些精力學習'PDO'或'mysqli'數據庫擴展。 [開始](http://php.net/manual/en/book.pdo.php) – RiggsFolly

回答

0

你是不是正確的這一說法在查詢串聯的價值,也文本數據,如本應在引號包裹

mysql_query("INSERT INTO text (textarea) VALUES ("mysql_real_escape_string($text)"); 

這是一個您的代碼的修正版本

mysql_query("INSERT INTO text 
        (textarea) 
      VALUES ('" . mysql_real_escape_string($text) . "')"); 

一個簡單的代碼是閱讀和可能維持將

$t = mysql_real_escape_string($text); 
mysql_query("INSERT INTO text (textarea) VALUES ('$t')"); 

我將是失職,如果我沒有提醒你,

每次你在新的使用the mysql_ 數據庫擴展代碼 a Kitten is strangled somewhere in the world它已被棄用,並已多年,並在PHP7中永遠消失。 如果你只是學習PHP,花費你的精力學習數據庫擴展PDOmysqliStart here

編輯RE:不將數據保存到數據庫

添加一些錯誤檢查你的代碼如下所示:

$t = mysql_real_escape_string($text); 
$result = mysql_query("INSERT INTO text (textarea) VALUES ('$t')"); 
if (! $result) { 
    echo mysql_error(); 
    exit; 
} 

一旦你知道錯誤(如果存在) ,你可以開始修復它。

+0

它似乎現在加載頁面,但它不會將文本放入數據庫表中。 –

+0

請參閱我的關於檢查錯誤的編輯 – RiggsFolly