0

hi , my server usually tells me that there is a syntax error so what is it?
the error is in line 12 but i dont know what is it
here is the code :

<?
$mysql_host = "mysql9.000webhost.com";
$mysql_database = "a2354076_post";
$mysql_user = "username";
$mysql_password = "password";
$tbl_name = "posts";
mysql_connect("$mysql_host","$mysql_user","$mysql_password")or die ("cannot connect to DB");
mysql_select_db("$mysql_database")or die("unable to select your database");
$datetime=date("y-m-d h:i:s");
$title=$_POST['title'];
$post=$_POST['post'];
$sql="INSERT INTO $tbl_name(datetime, title, post)values("$datetime","$title","$post")";
$result=mysql_query($sql);
if($result)
{
echo 'success !!!';
echo "<BR>your post is added successfully";
echo "<BR><a href='vew-posts.php'>View posts</a>";
}
else
{
echo "ERROR, your post is not added , please try again or check your database stetup , Awah Mohamad";
}
mysql_close();
?>

Edited by Awah Mohamed: n/a

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by metalix
0

Please try this:

$sql="INSERT INTO $tbl_name (`datetime`, `title`, `post`) values ('".$datetime."','".$title."','".$post."')";

0

just to clarify when you insert any value or query any value in a database. then need to have single quotes around them.
the columns don't need to but some put them in anyway.
for example if your title was cheese. it would be:
$sql = "INSERT INTO table (title) values ('cheese')";

therefore your $values must also be wrapped in single quotes, even though they are already in double quotes.
so both the above responses are correct. however using single quotes like in the second reply is messy as everything is appended.

hope this helps :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.