Click to See Complete Forum and Search --> : How to capture the current date?


caynada
07-04-2007, 02:35 PM
Please bare with me as I'm fairly new to all this. I'm trying to have the current date inserted into my DB in a field called "RequestDate" when the contact form is submitted. Can you please look at my code and tell me where I've gone wrong.

Thanks!

[CODE]

<?php
$FirstName = $HTTP_POST_VARS['FirstName'];
$LastName = $HTTP_POST_VARS['LastName'];
$Email = $HTTP_POST_VARS['Email'];
$Phone = $HTTP_POST_VARS['Phone'];
$interest = $HTTP_POST_VARS['interest'];
$Address = $HTTP_POST_VARS['Address'];
$Address2 = $HTTP_POST_VARS['Address2'];
$City = $HTTP_POST_VARS['City'];
$State = $HTTP_POST_VARS['State'];
$Country = $HTTP_POST_VARS['Country'];
$Zip = $HTTP_POST_VARS['Zip'];
$Referral = $HTTP_POST_VARS['Referral'];
$Comments = $HTTP_POST_VARS['Comments'];
$newsletter = $HTTP_POST_VARS['newsletter'];
$RequestDate = date("Y-m-d");

if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $Email)){
$error_Email = "Email address invalid";
}


if ((strlen(trim($FirstName)) < 2) || (strlen(trim($FirstName)) > 30))
{
$error_FirstName="First name is missing";
}

if ((strlen(trim($LastName)) < 2) || (strlen(trim($LastName)) > 30))
{
$error_LastName="<span class='error'>Surname is missing</span>";
}

if ((strlen(trim($Email)) < 2) || (strlen(trim($Email)) > 30))
{
$error_Email="<span class='error'>Email is missing</span>";
}






// IF Errors reshow the form page displaying the errors
if (($error_Email != "") || ($error_FirstName != "") || ($error_LastName != ""))
{ ?>


<!-- in the body of my page I have this code -->


<?php
//Else do the encryption
}
else
{


// DF: Added this to tidy-up the email sent out
$mailtext .= "<br />Request Date and Time = " . date("l dS of F Y h:i:s A");
$mailtext .= "<br /><br /><b>Request by:</b>";
$mailtext .= "<br />Name: ".$HTTP_POST_VARS["FirstName"]."&nbsp;&nbsp;".$HTTP_POST_VARS["LastName"]."<br />";
$mailtext .= "Email: ".$HTTP_POST_VARS["Email"]."<br />";
$mailtext .= "Phone: ".$HTTP_POST_VARS["Phone"]."<br /><br /><br />";
$mailtext .= "<b>Mailing Address:</b><br />";
$mailtext .= "Address: ".$HTTP_POST_VARS["Address"]."<br />";
$mailtext .= "&nbsp;".$HTTP_POST_VARS["Address2"]."<br />";
$mailtext .= "City/Town: ".$HTTP_POST_VARS["City"]."<br />";
$mailtext .= "State/Provence: ".$HTTP_POST_VARS["State"]."<br />";
$mailtext .= "Country: ".$HTTP_POST_VARS["Country"]."<br />";
$mailtext .= "Zip: ".$HTTP_POST_VARS["Zip"]."<br /><br /><br />";
$mailtext .= "Referral: ".$HTTP_POST_VARS["Referral"]."<br /><br />";
$mailtext .= "<b>Comments / Information request:</b><br /> ".$HTTP_POST_VARS["Comments"]."<br /><br />";
$mailtext .= "<b>Newsletter:</b><br />";
$mailtext .= "Would you like to receive our newsletter: ".$HTTP_POST_VARS["newsletter"]."<br />";




$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
//$headers .= "Content-Transfer-Encoding; 7 bit\r\n";
//$headers .= "From: " . $from . "\r\n";

if (!mail("xxx@xxxx.com","Online request from www.xxxxx.com",$mailtext, $headers)) {
$error;
} else {
//echo $mailtext;
$success;
echo "<p class=copy align=center style=copy><img src=../images/thankyou_box_02.gif /><br><br><a href=\"../index.htm\">Return Home</a></p></div>";
}
}
?>

<?php
//connect to the DB
$con = mysql_connect("localhost","xxxxxx","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xxxxxx", $con);
$sql="INSERT INTO ClientContact (interest, FirstName, LastName, Address, Address2, City, State, Zip, Country, Phone, Email, Referral, Comments, newsletter, RequestDate)
VALUES
('$_POST[interest]','$_POST[FirstName]','$_POST[LastName]','$_POST[Address]','$_POST[Address2]','$_POST[City]','$_POST[State]','$_POST[Zip]','$_POST[Country]','$_POST[Phone]','$_POST[Email]','$_POST[Referral]','$_POST[Comments]', '$_POST[RequestDate]','$_POST[newsletter]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
//echo "Your information has been sent.";
mysql_close($con)
?>



[CODE]

bluestars
07-04-2007, 06:03 PM
RequestDate wasn't the last variable in your value list, while it was in your field list.

caynada
07-04-2007, 06:17 PM
Thanks for that.

Still doesn't seem to be inserting the date into the DB. In the DB structure the field name is: RequestDate, the type is: date, Default is: 0000-00-00

Is there anything else you can see wrong?

Thanks again!

temp.user123
07-04-2007, 09:45 PM
MySQL probably doesn't recognize your date's string format. To insert the current date into a table column it is easiest to use one of MySQL's intrinsic functions to do so.

"Update `TableName` Set `DateColumn` = NOW() Where `IdColumn` = $id;"

caynada
07-05-2007, 07:23 AM
Thank you for your help. I'm not sure what you mean though, how does that fit into the above code?

Thanks again! I apologise, I'm just learning...

Lubox
07-05-2007, 08:01 AM
Like this?


$sql="INSERT INTO ClientContact (interest, FirstName, LastName, Address,
Address2, City, State, Zip, Country, Phone, Email, Referral, Comments, newsletter, RequestDate)
VALUES
('".$_POST[interest]."','".$_POST[FirstName]."','".$_POST[LastName]."','".$_POST[Address]."',
'".$_POST[Address2]."','".$_POST[City]."','".$_POST[State]."','".$_POST[Zip]."',
'".$_POST[Country]."','".$_POST[Phone]."','".$_POST[email]."','".$_POST[Referral]."',
'".$_POST[Comments]."', '".$_POST[newsletter]."',NOW())";


Lubox

temp.user123
07-05-2007, 08:08 AM
My example was just to show one of the MySQL Intrinsic functions you can use to set date columns. You can use the same function in your INSERT statement.

Looking at your code, though... You are using this statement to create a date variable:

$RequestDate = date("Y-m-d");

But, in your INSERT statement, you are doing this:

'$_POST[RequestDate]'

Seems that should be this, instead:

'$RequestDate'

That is, of course, if MySQL accepts that date string format.

caynada
07-05-2007, 09:25 AM
Thanks everyone! Lubox that did the trick, thanks very much!!