Store and display link click counts to/from MySQL Database?
Hi,
I'm working on a download script and I was wondering if anyone could tell me how to store the amount of times a file has been downloaded to a database. I have an understanding of how to display it, I just don't know what to do to add up the hits. If anyone can help me, I'd greatly appreciate it.
I'm working on a download script and I was wondering if anyone could tell me how to store the amount of times a file has been downloaded to a database. I have an understanding of how to display it, I just don't know what to do to add up the hits. If anyone can help me, I'd greatly appreciate it.
Thanks.
Yor mysql table have to contain a field named hits
for example :
id
url
author
hits
in a file called download.php you will executate 2 mysql queries. i supposed that your mysql table is called downloads
PHP Code:
$sql = "SELECT * from downloads WHERE id=$id"; //Here $id is the id taken from the url f.e download.php?id=23
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$hits = $row['hits']; //here is the number of hits
//now increase the number of hits
$hits = $hits + 1;
//lets upadate it
$sql = "UPADATE downloads SET hits="$hits" WHERE id=$id";
mysql_query($sql);
$sql = "SELECT * from downloads WHERE id=$id"; //Here $id is the id taken from the url f.e download.php?id=23
if (!$result = mysql_query($sql)) {
echo "Error: " . mysql_error();
}
$row = mysql_fetch_assoc($result);
$hits = $row['hits']; //here is the number of hits
//now increase the number of hits
$hits = $hits + 1;
//lets upadate it
$sql = "UPADATE downloads SET hits="$hits" WHERE id=$id";
if (!mysql_query($sql)) {
echo "error" . mysql_error();
}
You should be secured that the variable $id is passed to the script because the sql query won't work without it and will show you that error