Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling NOW() function in MySQL

I'm using php for making queries for mysql. Here is one:

UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = "$resEmail"

curDate - DateTime type. The problem is that after this query curDate of given email is

0000-00-00 00:00:00

What's wrong?

like image 901
Max Frai Avatar asked May 25 '26 05:05

Max Frai


1 Answers

Your PHP probably looks like this now:

$sql = 'UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = "$resEmail"';

The single quotes prevent the variable's value from being substituted into the string. You will need to change it to this:

$sql = "UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = '$resEmail'";

You should also be aware that you may have an SQL injection vulnerability here. Consider using mysql_real_escape_string to escape the email address.

$sql = "UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = '" .
       mysql_real_escape_string($resEmail) . "'";
like image 137
Mark Byers Avatar answered May 26 '26 17:05

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!