I have a problem with this code for make a hit counter per user profile on PDO (MYSQL), on page loads not update and not show the counter, only text "Visits:", the value remains at "0".

$id = $profile_data['username'];

$statement = $db->query("SELECT `visits` FROM `users` WHERE id='$id'");
$record = $statement->fetchAll();

if(sizeof($record) != 0)
{
 $counter = $record[0]['counter']++;
 $db->exec("UPDATE `users` SET visits='$counter' WHERE id='$id'");
 echo "Visits: " .$counter;
}
else
{
 $db->exec("INSERT INTO `users` (id, visits) VALUES ('$id', 1)");
 echo "Visits: 1";
}
有帮助吗?

解决方案

Since you pulling the fields visits from the database, you need to change:

$counter = $record[0]['counter']++;

to:

$counter = $record[0]['visits']++;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top