I wanted to add variable with date, to add to my date some days. But seems it not working at all.
$userVal='2014-11-06 20:52:05';
mysql_query("UPDATE users SET validity = DATE_ADD($userVal, INTERVAL $someDays DAY) WHERE id = $id");
I wanted to add variable with date, to add to my date some days. But seems it not working at all.
$userVal='2014-11-06 20:52:05';
mysql_query("UPDATE users SET validity = DATE_ADD($userVal, INTERVAL $someDays DAY) WHERE id = $id");
Do you have a problem with this?
UPDATE users
SET validity = DATE_ADD(NOW(), INTERVAL $someDays DAY)
WHERE id = $id;
You really don't need to pass in the current date/time. You should be using parameterized queries and mysqli or PDO, but those are other matters.
If you need to set it to a specific time you can use PHP to do the date addition:
$someDays = 2;
$userVal='2014-11-06 20:52:05';
$newUserVal = date("Y-m-d H:i:s", strtotime('+ '.$someDays.' days', strtotime($userVal)));
mysql_query("UPDATE users SET validity = '$newUserVal' WHERE id = $id");
Or if you need to update the existing validity
value:
mysql_query("UPDATE users SET validity = DATE_ADD(validity, INTERVAL $someDays DAY) WHERE id = $id");
There can be very good reasons not to use NOW(). The query-result using NOW() will never be cached by mysql, since NOW() is different all the time.
The problem with your query would be visible if you would debug it, but printing it to the screen:
$query = "UPDATE users SET validity = DATE_ADD($userVal, INTERVAL $someDays DAY) WHERE id = $id";
die($query);
Which would display, for example:
UPDATE users SET validity = DATE_ADD(2014-11-06 20:52:05, INTERVAL 1 DAY) WHERE id = 123
The problem is that the date you insert belongs between quotes, so change it to:
$query = "UPDATE users SET validity = DATE_ADD('$userVal', INTERVAL $someDays DAY) WHERE id = $id";
Note that the guys are right about SQL-injection though.