如何将CKEditor数据存储到MySQL中?

时间:2011-08-08 19:08:29

标签: php mysql ckeditor

我有CKEditor并希望将其数据发送到服务器以便将其存储在MySQL中。我能够使用jQuery Ajax将CKEditor的源代码(数据)发送到服务器但是当我尝试执行我的SQL查询以将其存储在数据库中时,insert命令会出错。我认为这里的主要问题是CKEditor数据中包含新行。如何解决这个问题?

我发现this类似于我的问题,但是当我尝试它并且它对我不起作用时。我不明白mysql_prep函数做了什么,因为它不是标准的PHP函数。

更新1

HTML表单的代码如下:

$("#save").click(function(){
    var data = $( '#editor1' ).val();                                        
    $.ajax({
        type:"POST",
        url:"save.php",
        data: "editor1="+data,
        timeout:15000, 
        error:function(XMLHttpRequest, textStatus, errorThrown){
          alert("ERROR");                            
        },
        success:function(response){
          alert("Success: "+response);
        }
    });                           
})

save.php有以下代码:

<?php
   include '../config.php'; //connecting to DB 
   $title='news 1'; $author=1; $date="2011/08/08"; $categories='1,2,3';     
   $short_text=$_POST['editor1']; $full_text=$_POST['editor1'];    
   $sql=sprintf('insert into news values(null,"%s",%d,"%s","%s","%s","%s")',$title,$author,$date,$short_text,$full_text,$categories);
   mysql_query($sql) or die("Error in $sql");
?>

2 个答案:

答案 0 :(得分:0)

Little bobby tables表示您非常容易受到SQL注入攻击。缓解SQL注入修复换行问题的最简单方法是使用预处理语句。至少你需要逃避你的输入,这也将解决换行问题。你可以使用mysql_real_escape_string,当然除了几年前已经弃用的,所以对于最近的工作你应该至少使用mysqli *函数(然后在你使用时使用准备好的查询)。

答案 1 :(得分:-1)

- 使用此

  

$ short_text = htmlspecialchars($ _ POST [&#39; editor1&#39;]);