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.
Using PHP to write date into form entry
Moderator: General Moderators
-
BritishAgent
- Forum Newbie
- Posts: 7
- Joined: Thu Jan 31, 2008 4:09 pm
Re: Using PHP to write date into form entry
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?
Is this the solution you were looking for?
- 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
BritishAgent is along the right lines.
Essentially, you just need to do something like this:
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.
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()");
?>
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
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.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?
-
BritishAgent
- Forum Newbie
- Posts: 7
- Joined: Thu Jan 31, 2008 4:09 pm
Re: Using PHP to write date into form entry
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?JamesRavenscroft wrote:BritishAgent is along the right lines.
Essentially, you just need to do something like this:
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.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()"); ?>
Hope this helps.
Thanks for your help - much appreciated.
Re: Using PHP to write date into form entry
Didn't you say that you've got the basic insert working already?? Just modify it as shown.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.