Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save ckeditor content in mysql database

I am using ckeditor to create a blog site. I want to use a combination of PHP and MySQL to save the entire article in a database. If I submit the form holding the ckeditor I get the editor's content in $_POST['editor']. I want to save the article in the MySQL database. The following image contains the entire form data. It consists of:

  1. title inside title element.

  2. article(text,code-snippet,image) inside "editor1" element.

$_POST['editor'] array looks like:

enter image description here

How can I save it to a mysql table? Can I save the entire editor1 element in a column of type TEXT?

like image 308
AL-zami Avatar asked Dec 05 '22 19:12

AL-zami


2 Answers

Yes, You can submit your entire article to the database. But, before you submit it; try to use this function:

function dataready($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
} 

Example:

 dataready($_POST['editor']);

So you can use whatever you want PDO or Mysqli.

To avoid echoing the HTML elements when you print the article, use this function:

  html_entity_decode($article_text);

Hope this works for you.

PS. using column type TEXT for your database is fine. You can also use LONGTEXT if you want to add more text to that column.

like image 139
weblover Avatar answered Dec 09 '22 15:12

weblover


Yes, you can store the Source Code in your database, that's how Wordpress works.

But remember to escape the strings before sending them to database, using:

mysqli_escape_string();

When you get the post data afterwards, the PHP will just print the HTML on the screen.

like image 23
Phiter Avatar answered Dec 09 '22 16:12

Phiter