Can't enter date in mysql DB

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
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Can't enter date in mysql DB

Post 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]
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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() )
Post Reply