Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

My register.php wont query the simplest of queries:

<?php
session_start();

/* Connect and Query database "accounts", then close connection */
$connection=mysql_connect("localhost","root","");
if (!$connection)
{
    die('Cannot connect to MySQL. Error: ' . mysql_error());
}

$check_database = mysql_select_db("accounts", $connection);
if (!$check_database)
{
    die('Cannot connect to database. Error: ' . mysql_error());
}

/* Query database to save user's post */
/* If field "repostid=0", then the post is not a repost; if the field "repostid>0", then the post is a repost with the field linking to the id of the post to be reposted */ 
$result = mysql_query("INSERT INTO posts2 (from) VALUES ('h1')");
if (!$result)
{
    die('Cannot query. Error: ' . mysql_error());
}

/* Close Connection */
mysql_close($connection);

/* Redirect to user's page */
header("Location: /website/index.php", 404);
exit();

?>

Here is the echo'd error message:

Cannot query. 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 'from) VALUES ('h1')' at line 1

It gets called from a form on post.php:

    <form name="postForm" action="userpost.php" method="post" enctype="multipart/form-data" onsubmit="validatePostForm()">
        <div id="posttext"><p>Enter the text you wish to post, then press "Post":</p></div>
        <div id="posttextarea"><textarea rows="10" cols="80" name="postinfo" onkeydown="characterTyping('postbuttoncharactersleft')"></textarea></div>
        <div id="postbutton"><input type="submit" value="Post"/></div><p id="postbuttoncharactersleft">250</p><p id="postbuttoncharacterslefttext"> characters left.</p>
    </form>

Here is the database table so you can confirm that it is the correct syntax for the query to this table:

enter image description here

share|improve this question

marked as duplicate by jprofitt, John Conde, Oscar Jara, andrewsi, sectus Nov 5 at 4:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

You need to escape reserved words in MySQL like from with backticks

INSERT INTO posts2 (`from`) VALUES ('h1')
share|improve this answer
 
I actually hit a reserved word? What's the chances... I feel so stupid. Thank you! –  shawn a Nov 4 at 21:38
 
@shawna For future reference: dev.mysql.com/doc/refman/5.6/en/reserved-words.html –  Daryl Gill Nov 4 at 21:49

Add the ; at the end of the SQL statement as well as the end of the PHP line, like this

$result = mysql_query("INSERT INTO posts2 (`from`) VALUES ('h1');");

Here is an example of SQL Insert:

INSERT INTO Customers (CustomerName, City, Country) VALUES ('Cardinal', 'Stavanger', 'Norway');

share|improve this answer
 
HAHA, didn't even notice the back ticks missing on his, I just put them in mine because of the from... Good catch juergen d –  SecureLive Nov 4 at 21:39
 
Also you don't have to put the semi-colon in the query like that, it works without it –  shawn a Nov 4 at 21:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.