All right Dipster… Because of the power outages my work was cancelled so I’m here to teach you how to do this…
You have the right idea, but you’re off just a bit…
make a file. Name it dlFile.php Now in this file you are going to want to do the following things:
-
Connect to your database.
-
Get the current count for the link pressed.
-
Add 1 to the count, and update the table with the new data
-
Redirect to the require page.
We’ll come back to that later. Now what you want to do is set up the database like this:
3 fields: path, hitID, and counter
path is going to be the link that you want to redirect to. Say, “http://www.kirupa.com”
hitID is going to be a unique identification number for that link. The best way to do this is so that it auto-increments (i’ll show you later) each time a new row is added to the table.
counter is pretty self-explanitory right?
Ok, say we start to fill up our database, so we have 2 entries. say it looks like this:

we’re going to use these as examples.
Now on your main page, you are going to have to set things up a bit differently than you normally would. Because you aren’t going to be sending them directly to their desination, you want them to stop off at dlFile.php first. So your links are going to look like this:
<a href="dlFile.php?hitID=001">Kirupa.com</a>
and you are going to be sending the hitID thru the URL string so the code in dlFile.php can grab it.
now lets get to the code in dlFile.php First we want to connect to the database:
<?
$hitID = $_GET['hitID']; //just grabs the variable
$linkID = mysql_connect("localhost", "userNAME", "passWORD");
mysql_select_db("dbNAME", $linkID);
That was easy enough, you know how to do that. Now lets do the query command to get the current counter value.
// Defines the query to run
$query = "SELECT * FROM hitsT WHERE hitID='$hitID'";
// Executes the query
$resultID = mysql_query($query, $linkID);
// Grab the data as an associative array
$row = mysql_fetch_assoc($resultID);
$count = $row['counter'] + 1; //defines out counter value
There are other ways to grab data, but I like using associative array for some reason. Its just what I use, other ways may work better for you.
Now we want to update the code
// Issue a new query updating the code...
$query2 = "UPDATE hitsT SET counter='$count' WHERE hitID='$hitID'";
//Execute the new query
$resultID2 = mysql_query($query2, $linkID);
That should have updated the code in the table, but we’re not done… we need to send the user on their way to their destination
// We can still use our array from our first query because we haven't
// overwritten it yet
// Close the DB since we are done with it
mysql_close($l);
// Send them to the page they want with header()
header("Location: ".$row['path']);
?>
Thats it for getting the info from the DB. Just in case my power goes out I’m going to save this post and then post again about how to make your database auto increment and I’ll show you how to enter all your links so you don’t have to do it by hand.