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
Solved

PHP Inserting today's date into MySQL

Posted on 2011-09-20
8
343 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…
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 look for a specific file type in a local or remote server directory using PHP.

808 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