Page 1 of 1

Using PHP to write date into form entry

Posted: Thu Jan 31, 2008 6:01 pm
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.

Re: Using PHP to write date into form entry

Posted: Thu Jan 31, 2008 11:38 pm
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?

Re: Using PHP to write date into form entry

Posted: Fri Feb 01, 2008 1:50 am
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.

Re: Using PHP to write date into form entry

Posted: Fri Feb 01, 2008 8:10 am
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.

Re: Using PHP to write date into form entry

Posted: Fri Feb 01, 2008 8:12 am
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.

Re: Using PHP to write date into form entry

Posted: Fri Feb 01, 2008 7:25 pm
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.