Using PHP to write date into form entry

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
BritishAgent
Forum Newbie
Posts: 7
Joined: Thu Jan 31, 2008 4:09 pm

Using PHP to write date into form entry

Post by BritishAgent »

I have very limited PHP and form experience, but I'm working on it.

I have managed to write a form that enters a user into a database and it works really nicely.

If I want to create a field that enters the date that the form was submitted into the database, how do I do that? I'd rather that the date box wasn't visible when the user hit the submit button, if possible, but was just entered straight into the database.

Thanks in advance for the help.
Festy
Forum Commoner
Posts: 28
Joined: Wed Jan 30, 2008 10:01 pm

Re: Using PHP to write date into form entry

Post by Festy »

you can achieve this with your MySql database alone. create a field say 'date' in your table. set the type of the field as 'datetime' or 'date' and MySql will insert the current date and time automatically each time a new entry is inserted into your database.
Is this the solution you were looking for?
User avatar
JamesRavenscroft
Forum Newbie
Posts: 10
Joined: Thu Jan 31, 2008 3:45 am
Location: West Midlands, United Kingdom

Re: Using PHP to write date into form entry

Post by JamesRavenscroft »

BritishAgent is along the right lines.

Essentially, you just need to do something like this:

Code: Select all

 
<?php
//using mysql_escape_string to avoid sql injection if you need to know
//what one of those is and why I'm doing that then use the security forum
$userdata1 = mysql_escape_string($_POST['userdata1']);
$userdata2 = mysql_escape_string($_POST['userdata2']);
 
 
mysql_query("INSERT INTO FOO(bar,fish, date) VALUES('{$userdata1}','{$userdata2}',NOW()");
 ?>
 
The important bit above is the NOW() bit. That is an internal function that tells mysql to insert the current date and time accurate to the second into the table. To do this, set field 'date' to a datetime or timestamp type field.

Hope this helps.
BritishAgent
Forum Newbie
Posts: 7
Joined: Thu Jan 31, 2008 4:09 pm

Re: Using PHP to write date into form entry

Post by BritishAgent »

Festy wrote:you can achieve this with your MySql database alone. create a field say 'date' in your table. set the type of the field as 'datetime' or 'date' and MySql will insert the current date and time automatically each time a new entry is inserted into your database.
Is this the solution you were looking for?
Thanks, I'll try that. If it's that simple, then that is absolutely the right solution I was looking for and I appreciate your help.
BritishAgent
Forum Newbie
Posts: 7
Joined: Thu Jan 31, 2008 4:09 pm

Re: Using PHP to write date into form entry

Post by BritishAgent »

JamesRavenscroft wrote:BritishAgent is along the right lines.

Essentially, you just need to do something like this:

Code: Select all

 
<?php
//using mysql_escape_string to avoid sql injection if you need to know
//what one of those is and why I'm doing that then use the security forum
$userdata1 = mysql_escape_string($_POST['userdata1']);
$userdata2 = mysql_escape_string($_POST['userdata2']);
 
 
mysql_query("INSERT INTO FOO(bar,fish, date) VALUES('{$userdata1}','{$userdata2}',NOW()");
 ?>
 
The important bit above is the NOW() bit. That is an internal function that tells mysql to insert the current date and time accurate to the second into the table. To do this, set field 'date' to a datetime or timestamp type field.

Hope this helps.
Thanks for this. Where would I put this on the form (as in, where on the page and how do I relate it to a field in the table) so that when the form is submitted this code knows what field to put it in?

Thanks for your help - much appreciated.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Using PHP to write date into form entry

Post by califdon »

BritishAgent wrote: Thanks for this. Where would I put this on the form (as in, where on the page and how do I relate it to a field in the table) so that when the form is submitted this code knows what field to put it in?

Thanks for your help - much appreciated.
Didn't you say that you've got the basic insert working already?? Just modify it as shown.
Post Reply