Manju
Manju

Reputation: 765

Insert ckeditor html code into the database

I am trying to insert the ckeditor text into database,I am able to print the code in html format,while inserting that into the db,iam getting the fallowing error

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'];
}
?>

please help me

Upvotes: 1

Views: 15719

Answers (5)

Himanshu Sharma
Himanshu Sharma

Reputation: 1

You can do i without using mysql_real_escape_string($content)

You can directly use $query = "INSERT INTO table VALUES('','one','$content')";

Upvotes: 0

Amol Londhe
Amol Londhe

Reputation: 11

It's work for me

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];
  }
  ?>

Upvotes: 1

Amol
Amol

Reputation: 343

If you are using your table name which is like any keywords from sql then take it in this symbol ` on the top of tab button

Upvotes: 0

Jenz
Jenz

Reputation: 8369

Closing braces for the query is missing.

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

Upvotes: 1

blue
blue

Reputation: 1949

Edit the line to read:

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

And make sure you're putting the right value for your table name in place of myTableName...

Upvotes: 1

Related Questions