将ckeditor html代码插入数据库

时间:2014-04-07 06:10:50

标签: php mysql ckeditor

我正在尝试将ckeditor文本插入到数据库中,我能够以html格式打印代码,同时将其插入到数据库中,我得到了以下错误

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 'table' at line 1

的index.php

<html>
<head>
<script type="text/javascript" src="ckeditor/smaple.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
<form action="action.php" method="post">
<textarea rows="20" cols="70" class="ckeditor" id="editor1" name="test1"></textarea>
<input type="submit" value="save">
</form>
</body>
</html>

action.php的

<?php
echo strip_tags($_POST['test1']);
$content  = $_POST['test1'];
mysql_connect("localhost","root","");
$conn = mysql_select_db("test");
$query = "INSERT INTO table VALUES('','one',mysql_real_escape_string($content)";
echo $query;
if(mysql_query($query))
{
    echo "inserted";
}
else
{
    mysql_error();
}
$display = "select * from table";
$res = mysql_query($display);
if($res)
echo "true";
else
echo mysql_error();
while ($result = mysql_fetch_array($res)) {
    echo $res['content'];
}
?>

请帮帮我

5 个答案:

答案 0 :(得分:1)

将该行编辑为:

$query = "INSERT INTO myTableName VALUES('','one','".mysql_real_escape_string($content)."'";

并确保为表名添加正确的值,以代替 myTableName ......

答案 1 :(得分:1)

缺少关闭查询的大括号。

$query = "INSERT INTO table VALUES('','one',mysql_real_escape_string($content))";

答案 2 :(得分:1)

这对我有用

的index.php

<html>
<head>
<script type="text/javascript" src="ckeditor.js"></script>
</head>
<body>
<form action="action.php" method="post">
<textarea rows="20" cols="70" class="ckeditor" id="editor1" name="test1">       </textarea>
 <input type="submit" value="save">
</form>
</body>
</html>

action.php的

<?php
echo strip_tags($_POST['test1']);
$content  = mysql_real_escape_string($_POST['test1']);
mysql_connect("localhost","root","");
$conn = mysql_select_db("test");
$query = "INSERT INTO yourtablename VALUES('','$content')";
echo $query;
if(mysql_query($query))
{
    echo "inserted";
 }
 else
{
mysql_error();
}
 $display = "select * from yourtablename";
 $res = mysql_query($display);
 if($res)
 echo "true";
 else
 echo mysql_error();
   while ($result = mysql_fetch_row($res)) {
   echo $result[1];
  }
  ?>

答案 3 :(得分:0)

如果你使用的是你的表名,就像sql中的任何关键字一样,那就把它放在标签按钮顶部的符号`

答案 4 :(得分:0)

你可以不使用mysql_real_escape_string($ content)

来做

您可以直接使用 $ query =“INSERT INTO table VALUES('','one','$ content')”;