Jump to content

Counting the number of downloads


graham23s

Recommended Posts

Hi Guys,

 

on my site when a user goes to the "downloads.php" page i update mysql to say downloads+1 to increment the number of downloads each time (but saying that not everyone who goes to the downloads page downloads something)

 

the code is pretty basic:

 

downloads.php

 

     // start by grabbing the id...//////////////////////////////////////////////////
     $id = $_GET['id'];	
     
     // get the file...///////////////////////////////////////////////////////////////////
     $query1 = "SELECT * FROM `uploads` WHERE `id`='$id'"; 
     $result1 = mysql_query($query1);
     $row = mysql_fetch_array($result1);
     
     $file_id = $row['file_id']; 
     $file_name = $row['file_name'];
     
     // update the hit counter...////////////////////////////////////////////////////////
     mysql_query("UPDATE `uploads` SET `downloads`=`downloads` + 1 WHERE `id`='$id'");

 

i was thinking a better way would be to place a button there when pressed the download box pops up and THEN it increments mysql but i'm not entirely sure where to go from there i can do the buttons ok:

 

<input type="submit" name="submit" value="downlod" />

 

so when the button is pressed the downloads starts (it's not a specific filename) so the name of the downloaded file changes depending on what the uploader has called it kinda thing.

 

any help would be great.

 

cheers

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/54283-counting-the-number-of-downloads/
Share on other sites

Try have a javascript popup that links straight to the file, see the code below:

if (isset($_POST['submit'])) {
?>
<script type="text/javascript">
window.open("http://www.yourdomain.com/file/link.exe");
</script>
<?
     // start by grabbing the id...//////////////////////////////////////////////////
     $id = $_GET['id'];	
     
     // get the file...///////////////////////////////////////////////////////////////////
     $query1 = "SELECT * FROM `uploads` WHERE `id`='$id'"; 
     $result1 = mysql_query($query1);
     $row = mysql_fetch_array($result1);
     
     $file_id = $row['file_id']; 
     $file_name = $row['file_name'];
     
     // update the hit counter...////////////////////////////////////////////////////////
     mysql_query("UPDATE `uploads` SET `downloads`=`downloads` + 1 WHERE `id`='$id'");}
?>

Try if this works or not, but I am not confident, just make sure the popup links to your file.

Ted

Hi Guys,

 

thanks for the input how hard is ajax? lol the only thing is i don't know the names of the files uploaded i couldn't say : a specificname.doc it would depend on what the user has uploaded would i just stick it in a variable?

 

cheers

 

Graham

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.