tags:

views:

43

answers:

1

I need help with this code, it doesent insert the values to my database. Probably a simple problem but it's late here in Sweden so I would appriciate if someone could have a look at this and tell me what's wrong:

    include "../../inc/mysql_config.php";
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);

mysql_query("INSERT INTO messages (to, message) VALUES ('".$to."', '".$message."')");

Every variable have an value, double checked that and the mysql_config.php is working.

MySQL error code:

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 'to, message) VALUES ('hubb', 'asd')' at line 1

+1  A: 

Something that will give you a clue is to use the mysql_error() function, e.g.

mysql_query("INSERT INTO messages (to, message) VALUES ('".$to."', '".$message."')") or die(mysql_error());

I would recommend using a more recent library such as MySQLi or PDO (preferably PDO). Both of these have support for prepared statements, which makes the whole string escaping thing much easier and much more consistent.

El Yobo
+1 for pdo. It's also database agnostic, so you don't have to learn a new set of functions if you choose to write against a different database in the future.
Kibbee
I'm working on a project where MySQL is the only option.El Yobo, I dont see any changes but the or die(mysql_error())...
Victor
That's right; the die(mysql_error()) is not intended to fix your query, just to tell you the what MySQL thinks the error is. If you can't tell from that message, please update your question with the error message and we'll help you out.
El Yobo
Updated and I still can't see the problem...
Victor
Can you show the schema for the messages table? Just add the output of "DESC messages".
El Yobo
And possibly printing out the whole statement that you're generating would be useful, i.e. copy and paste the mysql_query() line but put echo instead of mysql_query(). This way we can see the actual SQL that you're generating.
El Yobo
Here: http://i49.tinypic.com/1zfy80.png
Victor
Echo instead of mysql_query:INSERT INTO messages (to, message) VALUES ('hubb', 'asd')
Victor
"to" is an integer in your schema, but your statement is passing it a string - this is probably your problem :)
El Yobo
Damn, I blame again on the night and that I'm tired, thanks for your time and help! :)
Victor
You're welcome - good luck :)
El Yobo
Changed "to" to text but it still don't work, any guess?
Victor
"to" is a special word in MySQL; use backticks around the word "to" instead and you'll be fine (i.e. "\`to\`") You might want to think about renaming it to "recipient" or something else instead.
El Yobo
Yes, it works correctly now! Thanks for the info, I had no idea of that, switched the "to" to "recipient" instead, thanks once again for the help :)
Victor
No problem - I should have spotted that first up rather than pointing you in the wrong direction :-/
El Yobo