Page 1 of 1

Posting data to the database

Posted: Fri Feb 04, 2011 4:26 pm
by taraxgoesxboom
I have a simple php code to post information into my database table named mail.

For some reason I keep getting this 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 'to, from, subject, message) VALUES('tara2', 'tara', 'test', 'dflakjhdfkjhsldjk' at line 1"

The variables are passing correctly because they print out onto the screen. I feel like it's something silly I'm missing but I just don't see it.

Here's my code:

Code: Select all

<?php
session_start(); 
include 'db.php';
?>

<html>

<body>

<?php

$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_SESSION['username'];

echo $to;
echo $from;
echo $subject;
echo $message;



mysql_query("INSERT INTO mail (to, from, subject, message)
		VALUES('$to', '$from', '$subject', '$message')") or die (mysql_error());
	
?>

</body>
</html>

Re: Posting data to the database

Posted: Fri Feb 04, 2011 5:43 pm
by danwguy
could be way wrong here but have you tried putting single quotes around the first set too? i.e.

Code: Select all

<?php
session_start(); 
include 'db.php';
?>

<html>

<body>

<?php

$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_SESSION['username'];

echo $to;
echo $from;
echo $subject;
echo $message;



mysql_query("INSERT INTO mail ('to', 'from', 'subject', 'message')
                VALUES('$to', '$from', '$subject', '$message')") or die (mysql_error());
        
?>

</body>
</html>
 
or write it all out like this...

Code: Select all

<?php
session_start(); 
include 'db.php';
?>

<html>

<body>

<?php

$to = $_POST['to'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$from = $_SESSION['username'];

echo $to;
echo $from;
echo $subject;
echo $message;



mysql_query("INSERT INTO mail SET to='".$to."', from='".$from."', subject='".$subject."', message='".$message."'") or die (mysql_error());
        
?>

</body>
</html>
 
like I said I could be wrong, but I don't see an error in your original code, so that's all I can think.

Re: Posting data to the database

Posted: Sun Feb 06, 2011 3:34 am
by ganesh_dabhade
Try the following

Code: Select all

mysql_query("INSERT INTO mail VALUES('$to', '$from', '$subject', '$message')") or die (mysql_error());
Let us know if the problem is solved.

Re: Posting data to the database

Posted: Sun Feb 06, 2011 8:33 am
by divedj
passing a string as value in single quotes like
mysql_query("INSERT INTO table ('field1', 'field2') VALUES ('entry1', 'entry2')");
will work.

I am not shure of any changes in php or mysql versions but if you pass variables as value you might use this:

Code: Select all

 mysql_query("INSERT INTO table ('field1', 'field2') VALUES ('".$var1."', '".$var2."')");

Re: Posting data to the database

Posted: Tue Feb 08, 2011 3:05 pm
by taraxgoesxboom
It's strange, but it ended up having to do with the length of my column names in the database. I changed to and from to to_user and from_user and it works just fine now.

Thank you all for the help though.