Solved

PHP Inserting today's date into MySQL

Posted on 2011-09-20
8
345 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
Technology Partners: 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!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

689 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