I'm trying to make a tracking system on my website that's very basic, with just the amount of people present recorded.
Unfortunately, the code below doesn't work. I checked the error logs in my server and basically here's the issue : After the first execute there are no entities found, even though there is already an entry with that value, and so the code goes straight to the "else" and then crashes because there is already an entry with that primary key. Can someone help me find why it doesn't find the entity on the first execute?
Here is the code :
$q = "SELECT date, amount FROM tracking WHERE date = ?";
$req = $bdd->prepare($q);
$req->execute(date("Y-m-d"));
$results = $req->fetchAll();
if (count($results) != 0){
$results["amount"] = $results["amount"] + 1;
$track = $bdd->prepare("UPDATE tracking SET amount = ? WHERE DATE(date) = ?");
$track->execute(array($results["amount"], date("Y-m-d")));
exit;
}
else{
$q = 'INSERT INTO tracking (date, amount) VALUES (:val1, :val2)';
$req = $bdd->prepare($q);
$req->execute(
[
"val1" => date(Y-m-d),
"val2" => 1,
]
);
}
Thanks