execute php on click/count banner clicks

I am trying to develop a script for rotating banners and I want to keep track of the amount of clicks that the banner has. To do this I put the following code into the onclick event on a banner after it chooses the rotating image

<?php include('sqlconnect.php'); $add1c=1+$data1['clicks']; //click $add1d=1+$data1['totalclicks']; //total if($add1c>=$limit1){ mysql_query("UPDATE banners Set rotating=0 Where banner_id='".$data1['banner_id']."'"); //turns off banner for day } mysql_query("UPDATE banners Set clicks=".$add1c." Where banner_id='".$data1['banner_id']."'"); //banner for click mysql_query("UPDATE banners Set totalclicks=".$add1d." Where banner_id='".$data1['banner_id']."'"); //totalclicks include('sqlcloseit.php'); ?>

Earlier I included a file where the $data1 was a sql query and I know that works because every time I refresh the page it counts it as a click because im guessing its executing the php when the page starts up. I know thats what php does but my question is how do people keep track of clicks in a sql database for a click when php only occurs when the page originally starts up?

There are a couple of ways of doing this, at work we use a redirect since javascript is limited to javascript enabled users. The way this works is the banner links to another page in your site like:

<a href="redir.php?banner=<?php echo $arrResult['banner_id']; ?>&redir=<?php echo urlencode($arrResult['banner_url']); ?>"><img src="path/to/banner.jpg" /></a>

then on that script you iterate your db table for that banner and redirect. You can also use javascript which is going to have to utilize ajax.

Sponsor our Newsletter | Privacy Policy | Terms of Service