0

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
asked Jan 5, 2014 at 22:01
2
  • 2
    You should be using prepared statements to prevent against SQL injection. Otherwise, you will end up like Bobby. Commented Jan 5, 2014 at 22:04
  • 1
    array keys should always be quoted Commented Jan 5, 2014 at 22:05

4 Answers 4

6

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
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Dont forget to set the quotes in the post value: $_POST['name_from_input_field']
Thank you, it's always the minor issues that have me stumped! will change the quotes too thanks user3158490
5

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())";
answered Jan 5, 2014 at 22:03

Comments

0

Try if the following option works:

GETDATE()
answered Jan 5, 2014 at 22:04

1 Comment

Why a GETDATE()? Removing the quotes is more then enough right?
-1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.