Solved

PHP Inserting today's date into MySQL

Posted on 2011-09-20
8
342 Views
Last Modified: 2012-05-12
I am trying to apply the current date/time stamp when adding a new record.  Now() gives this error:
Call to undefined function now() in /homepages/4/d343334581/htdocs/P/dsummary.php on line 52

Here is code from line 52:

                  $sql = "INSERT into Donor (UserName, UserPW, CommunityId, FName, LName, Street1, Street2, City, State, Zip, JoinDt) values ('".mysql_real_escape_string($_POST["Wusername"])."', '".mysql_real_escape_string($_POST['Wpassword'])."', '".$_SESSION['CommunityId']."', '".mysql_real_escape_string($_POST['firstname'])."', '".mysql_real_escape_string($_POST['lastname'])."', '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($_POST['address2'])."', '".mysql_real_escape_string($_POST['city'])."', '".mysql_real_escape_string($_POST['state'])."', '".mysql_real_escape_string($_POST['zip'])."', ".now().")";

What do I need to fix?
0
Comment
Question by:kkbenj
  • 4
  • 2
  • 2
8 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36570733
Use the MySQL function NOW() instead.  

 $sql = "INSERT into Donor (UserName, UserPW, CommunityId, FName, LName, Street1, Street2, City, State, Zip, JoinDt) values ('".mysql_real_escape_string($_POST["Wusername"])."', '".mysql_real_escape_string($_POST['Wpassword'])."', '".$_SESSION['CommunityId']."', '".mysql_real_escape_string($_POST['firstname'])."', '".mysql_real_escape_string($_POST['lastname'])."', '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($_POST['address2'])."', '".mysql_real_escape_string($_POST['city'])."', '".mysql_real_escape_string($_POST['state'])."', '".mysql_real_escape_string($_POST['zip'])."', NOW())";
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 400 total points
ID: 36570736
There isn't a PHP function called now() but there is one called date() that returns a timestamp.
0
 

Author Comment

by:kkbenj
ID: 36570979
The field in the database is DATETIME with this format: 0000-00-00 00:00:00

Now the error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '20:37:45)' at line 1

And code:
$sql = "INSERT into Donor (UserName, UserPW, CommunityId, FName, LName, Street1, Street2, City, State, Zip, JoinDt) values ('".mysql_real_escape_string($_POST["Wusername"])."', '".mysql_real_escape_string($_POST['Wpassword'])."', '".$_SESSION['CommunityId']."', '".mysql_real_escape_string($_POST['firstname'])."', '".mysql_real_escape_string($_POST['lastname'])."', '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($_POST['address2'])."', '".mysql_real_escape_string($_POST['city'])."', '".mysql_real_escape_string($_POST['state'])."', '".mysql_real_escape_string($_POST['zip'])."', '".date('Y-m-d H:i:s')."')";

I've tried both single quotes and no quotes.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 11

Expert Comment

by:maeltar
ID: 36571702
Looks like the last  inverted comma was in the wrong place.

try :

$sql = "INSERT into Donor (UserName, UserPW, CommunityId, FName, LName, Street1, Street2, City, State, Zip, JoinDt) values ('".mysql_real_escape_string($_POST["Wusername"])."', '".mysql_real_escape_string($_POST['Wpassword'])."', '".$_SESSION['CommunityId']."', '".mysql_real_escape_string($_POST['firstname'])."', '".mysql_real_escape_string($_POST['lastname'])."', '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($_POST['address2'])."', '".mysql_real_escape_string($_POST['city'])."', '".mysql_real_escape_string($_POST['state'])."', '".mysql_real_escape_string($_POST['zip'])."'," NOW());

Open in new window

0
 
LVL 11

Assisted Solution

by:maeltar
maeltar earned 100 total points
ID: 36571709
OOps sorry it's early, I put it in the rong place as well !!!

$sql = "INSERT into Donor (UserName, UserPW, CommunityId, FName, LName, Street1, Street2, City, State, Zip, JoinDt) values ('".mysql_real_escape_string($_POST["Wusername"])."', '".mysql_real_escape_string($_POST['Wpassword'])."', '".$_SESSION['CommunityId']."', '".mysql_real_escape_string($_POST['firstname'])."', '".mysql_real_escape_string($_POST['lastname'])."', '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($_POST['address2'])."', '".mysql_real_escape_string($_POST['city'])."', '".mysql_real_escape_string($_POST['state'])."', '".mysql_real_escape_string($_POST['zip'])."', NOW())";

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36571715
'echo' your $sql string to see where the flaw is.  From the error message, it looks like the punctuation is off.  Is there some reason you don't like the MySQL function NOW()?
0
 

Author Comment

by:kkbenj
ID: 36572565
DaveBaldwin,

I am confused.  I originally tried to use NOW() but you replied:
DaveBaldwin:
There isn't a PHP function called now() but there is one called date() that returns a timestamp.

and I got the date() function to work as this (just the last piece of $sql1):
."', '".date('Y-m-d H:i:s')."')";

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36575054
Thanks for the points and glad you got it working.  In your original post, using ".now().")"; with the dots made it a PHP function or at least an attempt.  If you look at my first post above, you'll see that I put NOW() as part of the text of the SQL statement without any dots to make it part of the PHP code.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Sessions in WordPress 8 31
Cannot call a static method on an Eloquent object 6 32
Really simple no curl. Send a post 3 times 4 25
Complex MySQL Query 2 18
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question