Page 1 of 1

Can't enter date in mysql DB

Posted: Fri Sep 02, 2005 4:19 pm
by vchris
Hey guys!

I got another question about php. I'm creating a page where users can submit their feedbacks about the site and I want to enter the current date at the same time but for some reason I get an error. It says my error is in my SQL query.

Here is my code:

Code: Select all

if(empty($errors)){
		
		//Insert feedback in database
		require_once('mysql_connect.php');
		
		//Query the database
		$query = "INSERT INTO feedback (from, message, dateadded) 
		VALUES ('$f', '$m', NOW() )";
		
		//Run query
		$result = @mysql_query($query);
		
		//If it ran OK
		if($result){
			echo 'Thank you for submitting your feedback.';
		} else {
			echo 'Your feedback was not submitted due to a system error.';
			echo '<p>' . mysql_error() . "<br><br>Query:" . $query . '</p>';
			include('footer.php');
			exit();
		}
		
		
		mysql_close();
	}
This is only part of the code I have. Once I click submit, the webpage only displays the else: Your feedback was not submitted..... and the error.

Here is the error message:

Code: Select all

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, message, dateadded) VALUES ('asdf', 'asdfsadfsadfdas',

Query:INSERT INTO feedback (from, message, dateadded) VALUES ('asdf', 'asdfsadfsadfdas', NOW() )
In my mysql db, I have a date field where I'm supposed to enter the date. I'm not 100% sure the error is the NOW() function. I also tried inputting the date manually and still doesn't work.


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Sep 02, 2005 4:23 pm
by raghavan20
'from' is a mysql keyword. use backticks from database, table and field names.

Code: Select all

Query:INSERT INTO feedback (from, message, dateadded) VALUES ('asdf', 'asdfsadfsadfdas', NOW() )
should be

Code: Select all

Query:INSERT INTO `feedback` (`from`, `message`, `dateadded`) VALUES ('asdf', 'asdfsadfsadfdas', NOW() )