0

Here is my php:

<?php
require included.php; //which contains the function getpost($post_id)
//once I use the getpost() function below to return a string which is the post 
//content, then I can not insert this new row to database.if I change it something 
//like $post_content='this is the post content' , it would insert a row to database

$post_content=getpost($post_id);
if($insert=mysql_query("INSERT INTO `join` VALUES('','username','$post_content')"))
{
   echo 'ok';
}
else{

   echo 'error';
}
?>

Here is the function getpost($post_id) in the included.php:

function getpost($post_id){

    $select_post=mysql_query("SELECT `post_text` FROM `user_post` WHERE `id`='$post_id' ");
return mysql_result($select_post,0,'post_text');
}

those data would be sent back to jquery AJAX request as data, and if I echo the $post_content = get_post($post_id), I can alert the right stuffs, it seems something wrong to use a function to get the post content then it can't be inserted into the database.

kesong
  • 309
  • 1
  • 5
  • 13
  • Remove the "R" in `'VALUERS'`, for one thing. And why would you name a table "join". That is a SQL keyword and will make for rather confusing looking queries. – Gordon Linoff Feb 08 '14 at 13:57

3 Answers3

0

The keyword for INSERT is VALUES not VALUERS:

if($insert=mysql_query("INSERT INTO `join` VALUERS('','username','$post_content')"))

Should be

if($insert=mysql_query("INSERT INTO `join` VALUES ('','username','$post_content')"))

If you had error checking in place, like using mysql_error() you would have caught this quickly.

Also, please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I typed wrong in my question, if I use $post_content='hello, wolrd', to assign value to the $post_content, the query would insert a row successfully – kesong Feb 08 '14 at 14:01
0

Typo -

...INSERT INTO `join` VALUERS('','usern...
                           ^
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
0

Maybe because you have a typo? INSERT INTO join VALUERS('','username','$post_content') should be INSERT INTO join VALUES('','username','$post_content') I can't see anything else wrong

vortexkd
  • 63
  • 9
  • yes,I typed it wrong in my question, if I use $post_content='hello, wolrd', to assign value to the $post_content, the query would insert a row successfully, but if I use the function getpost() to return content and assign to the $post_content,it won;t work – kesong Feb 08 '14 at 14:12