i've made some query, i have to put in a datetime field the value of $date.
I don't know what's wrong with my queries, did i do something wrong ?
I checked my fields and all of them are correct.
Thanks
<?php
$duration = $custom['duration'];
$uid = $custom['uid']; // id of the user, can be 1,2,3 ...
$STH = $bdd->prepare('SELECT user_id FROM transactions WHERE user_id = :uid');
$STH->bindParam(':uid', $uid, PDO::PARAM_INT);
$STH -> execute();
$number_of_rows = $STH->fetchColumn();
$result = $STH -> fetch();
// On vérifie si l'user à déjà un compte prénium
if ($number_of_rows) > 0) {
$end = $result["end_subscription"]; // it's a datetime field
$date = new DateTime($end);
}else{
$date = new DateTime();
}
$date = new DateInterval('P'.$duration.'M');
$date = format('Y-m-d H:i:s');
$stmt = $bdd->prepare("UPDATE users SET end_subscription = ':end_subscription' WHERE id = ':uid'");
$stmt->bindParam(':end_subscription', $date, PDO::PARAM_STR);
$stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
$stmt->execute();
?>
Don't put the placeholder in quotes:
$stmt = $bdd->prepare("UPDATE users
SET end_subscription = :end_subscription
WHERE id = :uid");
You were setting the column to the literal string :end_subscription
rather than substituting the parameter.
And your code to format the date is incorrect. It should be:
$interval = new DateInterval('P'.$duration.'M');
$date = new DateTime();
$future_date = $date->add($interval);
$date_string = $future_date->format('Y-m-d H:i:s');