Reputation: 21237
I have a tinymce textarea where I put in some default text as the page is loaded. This text is formatted as bold faced and underlined. All of this works well. However, when I go to store this text in a MySQL database, I get the following error:
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
Here is how I am creating this default text inside of the textarea;
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>");
}
When I take this code out and just type the text in manually (along with formatting the text using the tinymce buttons), everything saves in the database perfectly. I don't know how to get this text to both appear with formatting AND be able to save it via a PHP/MySQL call.
Does anyone know how to do this? Thanks!
EDIT: Here is the relevant PHP code. Note that (as previously mentioned) this code works just fine if I manually type and format the text. However, it fails when I use javascript to set up the text for me.
$sql = "INSERT INTO Emails
(date, subject, greeting, results, upcoming, thisweek, signoff)
VALUES
('$today', '$subject', '$greeting', '$results', '$upcoming',
'$thisweek', '$signoff')";
$result = mysql_query($sql);
EDIT 2: Here is the echo of the $sql text. What look strange to me is all of the carriage returns, which must come from the 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 ')
Upvotes: 1
Views: 3861
Reputation: 8593
I use the below function so you would need to convert your mysql to mysqli ... which you need to do anyhow or you code will not work with upcoming releases. LINK to help http://php.net/manual/en/mysqli.query.php
Also note this code it modified as I have it in classes so it might be a little off ... I don't use global etc etc.
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) . "')";
Upvotes: 2