2013-07-23 95 views
1

我有一個tinymce textarea,我在頁面加載時放入了一些默認文本。該文本格式爲粗體和下劃線。所有這一切運作良好。然而,當我去商店在MySQL數據庫中這段文字,我得到以下錯誤:將tinymce的格式化文本存儲在數據庫中

Error adding email to database: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ... at line 5

這裏是我如何創建文本區域的這裏面默認的文本;

window.onload = function formatText() { 
    tinyMCE.get("results").setContent("<b><u>RESULTS</b></u><br><br><br>"); 
    tinyMCE.get("upcoming_races").setContent("<b><u>UPCOMING EVENTS</b></u><br><br><br>"); 
    tinyMCE.get("thisweek").setContent("<b><u>THIS WEEK'S TRAINING</b></u><br><br><br>"); 

} 

當我出去借這個代碼,只是在手動鍵入文本(使用TinyMCE的按鈕格式化文本一起),一切都在數據庫中保存完美。我不知道如何讓這兩個文本都顯示格式並能夠通過PHP/MySQL調用保存它。

有誰知道如何做到這一點?謝謝!

編輯:這裏是相關的PHP代碼。請注意(如前所述),如果我手動鍵入並格式化文本,則此代碼工作得很好。但是,當我使用javascript爲我設置文本時失敗。

$sql = "INSERT INTO Emails 
      (date, subject, greeting, results, upcoming, thisweek, signoff) 
      VALUES 
      ('$today', '$subject', '$greeting', '$results', '$upcoming', 
      '$thisweek', '$signoff')"; 

$result = mysql_query($sql); 

編輯2:這是$ sql文本的回聲。什麼表情很奇怪,我是所有的回車,這必須來自的javascript:

INSERT INTO Emails (date, subject, greeting, results, upcoming, thisweek, signoff) VALUES ('2013-07-23', 'test', ' test ', ' RESULTS

', ' UPCOMING EVENTS

', ' THIS WEEK'S TRAINING

', ' test signoff ')

+0

你的錯誤是不你的JS代碼請發佈相關的PHP代碼,以便我們可以看看。 – cmorrissey

+0

完成。往上看。 – Alex

+0

好吧,這將是艱難的調試,但讓我們開始添加echo $ sql;就在你的$ result =之前,用一個給你這個錯誤的字符串。用你的帖子更新你的帖子,或者把它放在評論中,我的猜測是你沒有正確地逃避一切。 – cmorrissey

回答

2

我用下面的功能,所以你需要你的mysql轉換爲mysqli的...你需要做的,無論如何或者你的代碼在即將發佈的版本中不起作用。 LINK幫助http://php.net/manual/en/mysqli.query.php

還應注意這個代碼,它修改爲我有在課堂所以它可能是有點過......我不使用全球等等等等

function escapeString($string) { 

    global $connection; 

    // depreciated function 
    if (version_compare(phpversion(),"4.3.0", "<")){ 
     return mysqli_escape_string($connection, $string); 
    } else { 
     return mysqli_real_escape_string($connection, $string); 
    } 

} 


$sql = "INSERT INTO Emails 
      (date, subject, greeting, results, upcoming, thisweek, signoff) 
      VALUES 
      ('" . escapeString($today) . "', '" . escapeString($subject. "', '" . escapeString($greeting) . "', '" . escapeString($results) . "', '" . escapeString($upcoming) . "', 
      '" . escapeString($thisweek) . "', '" . escapeString($signoff) . "')"; 
相關問題