Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date

Status
Not open for further replies.

Rhyan

Programmer
Jul 4, 2000
33
US
How can I have a form that automatically sends the current date to a database (mysql)?
 
Hi Rhyan,
Just use the NOW() function .


$SQL = "INSERT INTO $table (name,mydate)
VALUES
('$name',NOW())";

mysql_query($SQL,$dbh);
 
Thanks for the info, but actually, I was thinking more along the lines of using the info in a form.

For example when someone sends this form, they are registering for something, and I want the date they register to show up in the table.

So in the form I have this, but its not working
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;Joined&quot; VALUE=&quot;(date (&quot;F dS Y&quot;))&quot;
 
Try this :
<?php
$cur_date = date(&quot;F dS Y&quot;);
?>

<Input type=hidden name=joined
value=&quot;<?php echo $cur_date?>&quot;>

Your problem is that your missing the &quot;<?php&quot; tags.

Cheers
 
hmm, I tried it like you suggested, still no date value sent to the database.

Heres the code I'm using now

<?php
$cur_date = date(&quot;F dS Y&quot;);

function user_logout() {
setcookie('user_name','',(time()+2592000),'/','',0);
setcookie('id_hash','',(time()+2592000),'/','',0);
}
include($DOCUMENT_ROOT.'/include/database.php3');
include($DOCUMENT_ROOT.'/include/user.php3');

if (user_isloggedin()) {
user_logout();
$user_name='';
}

if ($submit) {
user_register($user_name,$password1,$password2,$email,$real_name);
}

if ($feedback) {
echo '<FONT COLOR=&quot;RED&quot;><H2>'.$feedback.'</H2></FONT>';
}

echo '<H3>Register With BAA Private Community</H3>
<P>
Carefully fill in this info and a confirmation email will be sent to you.
<P>
Please write down your log in information so that you do not have trouble accessing the Private Community at a later date <P>
<FORM ACTION=&quot;'. $PHP_SELF .'&quot; METHOD=&quot;POST&quot;>
<B>Real Name:</B><BR>
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;real_name&quot; VALUE=&quot;'. $real_name .'&quot; SIZE=&quot;20&quot; MAXLENGTH=&quot;35&quot;>
<P>
<B>User Name:</B><BR>
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;user_name&quot; VALUE=&quot;'. $user_name .'&quot; SIZE=&quot;10&quot; MAXLENGTH=&quot;15&quot;>
<P>
<B>Password:</B><BR>
<INPUT TYPE=&quot;password&quot; NAME=&quot;password1&quot; VALUE=&quot;&quot; SIZE=&quot;10&quot; MAXLENGTH=&quot;15&quot;>
<P>
<B>Password (again):</B><BR>
<INPUT TYPE=&quot;password&quot; NAME=&quot;password2&quot; VALUE=&quot;&quot; SIZE=&quot;10&quot; MAXLENGTH=&quot;15&quot;>
<P>
<B>Email (Required - Must be accurate to confirm):</B><BR>
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;email&quot; VALUE=&quot;'. $email .'&quot; SIZE=&quot;20&quot; MAXLENGTH=&quot;35&quot;>
<P>
<Input type=hidden name=Joined
value=&quot;'.$cur_date.'&quot;>
<P>
<INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;submit&quot; VALUE=&quot;Send My Confirmation&quot;>
</FORM>';




?>

I've also tried it like you said, still didnt work
<Input type=hidden name=Joined
value=&quot;<?php echo $cur_date?>&quot;>
 
Hi Rhyan,

Does the date show on the form ?
view the html source and see if the date is shown.

if it does then the $Joined var is being reset in
your php insert script or the format of $Joined
is incorrect for Mysql.

can you show your SQL insert query?
Try printing the $Joined var just before you
submit the query.

If your date field is of type DATE , you may need
to reformat the $Joined var before inserting
it into the database.
I know that mysql DATE field takes 'yyyy-mm-dd'
format.

good luck

 
Yes you are right, I viewed source and <Input type=hidden name=Joined value=&quot;August 25th 2000&quot;>

And you are also correct that the Joined Field is of Date and is in this format 0000-00-00

I guess this explains why no info is being sent to db. Geeze, I thought this would something simple to do but its turning out to be quite the opposite.

So Basically I'm sending this to the database which it wont accept so it needs to be reformatted?
date(&quot;F dS Y&quot;); to the 0000-00-00 format, but how?

Then to make things more ridiculous, I have a page on which I will use to display the name and JOINED date. I wanted it in the August 25th 2000 not the 2000-08-25 format lol Does that mean I have to change it again there in that script? Man I'm confused
 
Not a problems :
first you need to send your date to mysql in the yyyy-mm-dd
format :

this is done with
- $cur_date = date (&quot;Y-m-d&quot;);
- put the above in you hidden field form.
- then do your normal insert

In you SQL statement use the Date_Format function:
SELECT * ,
DATE_FROMAT(date_field,'%M %D %Y') AS joined_date
FROM my_table ;

Check the manual for other date formats.

Cheers




 
Hmmm, I did as you said, but I still don't get a date value in the database upon form submission :-(

<?php
function user_logout() {
setcookie('user_name','',(time()+2592000),'/','',0);
setcookie('id_hash','',(time()+2592000),'/','',0);
}
include($DOCUMENT_ROOT.'/include/database.php3');
include($DOCUMENT_ROOT.'/include/user.php3');

if (user_isloggedin()) {
user_logout();
$user_name='';
}









if ($submit) {
user_register($user_name,$password1,$password2,$email,$real_name);
}

if ($feedback) {
echo '<FONT COLOR=&quot;RED&quot;><H2>'.$feedback.'</H2></FONT>';
}

echo '<H3>Register With BAA Private Community</H3>
<P>
Carefully fill in this info and a confirmation email will be sent to you.
<P>
Please write down your log in information so that you do not have trouble accessing the Private Community at a later date <P>
<FORM ACTION=&quot;'. $PHP_SELF .'&quot; METHOD=&quot;POST&quot;>
<B>Real Name:</B><BR>
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;real_name&quot; VALUE=&quot;'. $real_name .'&quot; SIZE=&quot;20&quot; MAXLENGTH=&quot;35&quot;>
<P>
<B>User Name:</B><BR>
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;user_name&quot; VALUE=&quot;'. $user_name .'&quot; SIZE=&quot;10&quot; MAXLENGTH=&quot;15&quot;>
<P>
<B>Password:</B><BR>
<INPUT TYPE=&quot;password&quot; NAME=&quot;password1&quot; VALUE=&quot;&quot; SIZE=&quot;10&quot; MAXLENGTH=&quot;15&quot;>
<P>
<B>Password (again):</B><BR>
<INPUT TYPE=&quot;password&quot; NAME=&quot;password2&quot; VALUE=&quot;&quot; SIZE=&quot;10&quot; MAXLENGTH=&quot;15&quot;>
<P>
<B>Email (Required - Must be accurate to confirm):</B><BR>
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;email&quot; VALUE=&quot;'. $email .'&quot; SIZE=&quot;20&quot; MAXLENGTH=&quot;35&quot;>
<P>
<Input type=hidden name=Joined
value=&quot;<?php $cur_date = date (&quot;Y-m-d&quot;) '.$cur_date.'; ?>&quot;>

<P>
<INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;submit&quot; VALUE=&quot;Send My Confirmation&quot;>
</FORM>';




?>

 
<Input type=hidden name=Joined
value=&quot;<?php $cur_date = date (&quot;Y-m-d&quot;) '.$cur_date.'; ?>&quot;>

the above is wrong , should be :
<Input type=hidden name=Joined
value=&quot;<?php echo date(&quot;Y-m-d&quot;);?>&quot;>

before you submit, make sure the date is in the
form by doing a view source on the html.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top