Solved

PHP Inserting today's date into MySQL

Posted on 2011-09-20
8
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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