423,311 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,311 IT Pros & Developers. It's quick & easy.

PHP form won't save date to database

P: 96
I'm trying to get a form submission to automatically save the date when submitted, but I suspect that one of my variables is not right.

There are these two variables:

Expand|Select|Wrap|Line Numbers
  1. $timestamp = $_POST['date'];
  2. $current_date = date('j/M/y', $timestamp);
  3.  
Then I'm inserting it like so:

Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['submit'])){
  2.     $query = "INSERT INTO news (`headline`, `body`, `image`, `date`) 
  3.     VALUES ('$headline', '$body', '$image', $current_date);";
  4.     $result = $mysqli->query($query);
  5. }
  6.  
I'm wondering if the $current_date variable is my problem and how to fix it.
Jun 11 '15 #1

✓ answered by computerfox

You don't have single quotes around the $current_date in the query.
Might I also suggest that you

Expand|Select|Wrap|Line Numbers
  1. alter table news modify column date timestamp default now();
  2.  
The date will then be automatic and you can remove that value from the query.
Also, you should never use that as a column name because there is a HIGH risk of MySQL being confused. Standard is last_updated_date.

Edit:
I see that you're actually getting the date from the form. Try using the single quotes around the variable and also I highly recommend changing the column name. You can call it news_date or something. I'm leaving the above post for future documentation.

Share this Question
Share on Google+
6 Replies


computerfox
100+
P: 278
You don't have single quotes around the $current_date in the query.
Might I also suggest that you

Expand|Select|Wrap|Line Numbers
  1. alter table news modify column date timestamp default now();
  2.  
The date will then be automatic and you can remove that value from the query.
Also, you should never use that as a column name because there is a HIGH risk of MySQL being confused. Standard is last_updated_date.

Edit:
I see that you're actually getting the date from the form. Try using the single quotes around the variable and also I highly recommend changing the column name. You can call it news_date or something. I'm leaving the above post for future documentation.
Jun 11 '15 #2

Dormilich
Expert Mod 5K+
P: 8,639
note: your code indicates that your date field is a (VAR)CHAR type, which is the worst choice you could make because you cannot do sensible date calculations in SQL.
Jun 12 '15 #3

computerfox
100+
P: 278
Dorm, that's not necessarily true. He could be formatting the date a certain way or he's paranoid that the date won't be displayed correctly. That could also be how the code he copy/pasted was built which is one of the main reasons a developer should build their stuff from scratch. And even if it was in varchar or char, he could still do date manipulation, but it would require a conversion which would be an extra step. We really can't assume since we don't have the database schema.
Jun 13 '15 #4

P: 96
I just checked the Db structure and it's actually 'timestamp'.
Jun 14 '15 #5

Dormilich
Expert Mod 5K+
P: 8,639
then $current_date = date('j/M/y', $timestamp); should not give you a correct value.
Jun 15 '15 #6

P: 96
That's (probably) correct. But, I removed the date from the insert completely. So, it's automatic like the id now.
Jun 15 '15 #7

Post your reply

Sign in to post your reply or Sign up for a free account.