I have tried to insert the current date using the now() syntax but it's not working. The column in MySQL is under datetime called date_added but when I check the command prompt all I get is 0000-00-00 00:00:00.
INSERT INTO FOOD (food_name, food_type, total, available, date_added)
VALUES
('$_POST[food_name]','$_POST[food_type]','$_POST[total]','$_POST[available]','NOW()')";
Chris
139k139 gold badges317 silver badges293 bronze badges
-
2You should be using prepared statements to prevent against SQL injection. Otherwise, you will end up like Bobby.Kermit– Kermit2014年01月05日 22:04:17 +00:00Commented Jan 5, 2014 at 22:04
-
1array keys should always be quoteduser557846– user5578462014年01月05日 22:05:46 +00:00Commented Jan 5, 2014 at 22:05
4 Answers 4
You need to remove the single quotes from NOW():
...
VALUES (
$_POST[food_name],
$_POST[food_type],
$_POST[total],
$_POST[available],
NOW())";
Kermit
34.1k13 gold badges89 silver badges121 bronze badges
answered Jan 5, 2014 at 22:03
Donny van V
9811 gold badge11 silver badges24 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Bram
I was a bit confused too, but the whole thing is a string in double quotes. The quotes around the $_POST should stay because those are SQL quotes. Not very beautiful though.
Donny van V
Dont forget to set the quotes in the post value: $_POST['name_from_input_field']
user3142622
Thank you, it's always the minor issues that have me stumped! will change the quotes too thanks user3158490
Remove the quotes around NOW(), in that way NOW() is treated as a SQL function instead of a string.
VALUES ('$_POST[food_name]','$_POST[food_type]','$_POST[total]','$_POST[available]',NOW())";
Comments
Try if the following option works:
GETDATE()
answered Jan 5, 2014 at 22:04
Jeroen Ketelaar
1021 silver badge7 bronze badges
1 Comment
Donny van V
Why a GETDATE()? Removing the quotes is more then enough right?
offtopic:
Is this possible with the array? Never gave it a shot:
$food= array('apple', 'Banana', 'Pineapple');
INSERT INTO food ('fruit_arrays') VALUES ($food)
answered Jan 5, 2014 at 22:15
Donny van V
9811 gold badge11 silver badges24 bronze badges
Comments
default