Solved

PHP Inserting today's date into MySQL

Posted on 2011-09-20
8
335 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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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 82

Accepted Solution

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

Author Comment

by:kkbenj
Comment Utility
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
 
LVL 11

Expert Comment

by:maeltar
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 11

Assisted Solution

by:maeltar
maeltar earned 100 total points
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
'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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now