Posting data to the database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
taraxgoesxboom
Forum Newbie
Posts: 5
Joined: Tue Jan 25, 2011 2:22 pm

Posting data to the database

Post 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>
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Posting data to the database

Post 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.
User avatar
ganesh_dabhade
Forum Newbie
Posts: 19
Joined: Sun Feb 06, 2011 12:42 am
Contact:

Re: Posting data to the database

Post 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.
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

Re: Posting data to the database

Post 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."')");
taraxgoesxboom
Forum Newbie
Posts: 5
Joined: Tue Jan 25, 2011 2:22 pm

Re: Posting data to the database

Post 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.
Post Reply