473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PHP form won't save date to database

97 64KB
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.

6 1495
computerfox
276 100+
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
8,658 Expert Mod 8TB
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
276 100+
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
tdrsam
97 64KB
I just checked the Db structure and it's actually 'timestamp'.
Jun 14 '15 #5
Dormilich
8,658 Expert Mod 8TB
then $current_date = date('j/M/y', $timestamp); should not give you a correct value.
Jun 15 '15 #6
tdrsam
97 64KB
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

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

Similar topics

1
by: eduardoben | last post by:
I use a tableadapter with the sql string = select * fom table where field=? I load the desired record in a form and populate textboxes but after i modify them when i use tableadapter.update it...
8
by: Mark12345 | last post by:
Hi folks, thanks for looking. I have a database that takes down order details. Each customer has there own priceid in the price table. the errors that I get say enter parameter value for:...
12
by: Birky | last post by:
How can I stop a form from updating the database when a user closes the form? I have a form which is used for referencing, additions, and modifications but every time the form is closed with no...
1
by: madhavarao | last post by:
capture form to save into JPEG format in windows application uisng C#.net. But i want to save full form instead of active window. Thanks in advance
10
by: Gilles Ganault | last post by:
Hello Out of curiosity, is there a smarter, easier way to read data sent by a form, and save them into a database? I have about 20 fields, and it'd be easier if I could just use a loop to go...
6
by: Chege | last post by:
I know this question had been asked before but i never got a solution. I have form bounded to a table, i would Not like this form to save any blanks. If i type something on text box than change my...
2
by: tqa89 | last post by:
Hi, When user input the date,I want to save it to database. But I'm not sure about the date format,whether I should change the format? I'm using getter and setter method to save to database.I'm...
10
by: tajuddin | last post by:
I wanted to have a Command Button on a form to save a record & then ask to open a new record. I am new to access. I have MDFID on a form with primary key. Thanks Seth, To be frank, I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.