Will the following code work? I can't test it for various reasons. It must, must work first time round, so please be thorough with your evaluations. Basically, I want to count how many clicks my links get. I've put my links into a database. When someone clicks a link on my site (which look like this: goto.php?id=1), they are sent to goto.php, which: gets the ID connects to the database updates the Count column of the link table selects the real link (note: this has a variable appended to it: http://www.whatever.com?=$var) redirects the visitor to the location <?php $id = intval($_GET['id']); $conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR); mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR); mysql_query("UPDATE table SET count = count+1 WHERE id=$id"); $href = "SELECT url FROM table WHERE id=$id"; $qry = mysql_query($href); list($href)=mysql_fetch_row($qry); // list() because an array is returned eval("\$href = \"$href\";"); header("Location:$href"); mysql_close($conn); ?> PHP: What I am particularly worried about are the variables appended to the links in the database. Will they be echoed properly?
Just to be clear . . . I'm not asking anyone to test this for me. All I want is for anyone to tell me if they can see any obvious errors.
You see, the only way for me to test this code properly is to click a link on my site, go to a store, buy something, and see if the variable in the URLs appears in my affiliate network account. All I'm asking, therefore, is if anyone can see any obvious errors. If not, that's great.
Always use mysql_fetch_assoc instead of mysql_fetch_row. This will avoid further complications and editing if you change the query. Peace,
if you use variables other then id, like p,page,s,q,cat... you need to modify your code for these variables. goto.php?id=1 goto.php?id=1&cat=5 goto.php?id=1&cat=6.. in these links there is cat variable other then id variable so your code will look like $id = intval($_GET['id']); $cat = intval($_GET['cat']); or another way without modification is you'll create a table with entry_id, url, count fields and make entry_id autoincrement and in the begining of your header file you will insert this code <?php //$id = intval($_GET['id']); $url = $_SERVER['QUERY_STRING']; $conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR); mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR); #control if url inserted before $control = mysql_result(mysql_query("SELECT entry_id FROM table where url='$url'"),0); if(empty($control)) { #if url not inserted before make insert mysql_query("INSERT into table VALUES(NULL,'$url','1')");//1 for first count } else { #if url inserted before make update mysql_query("UPDATE table SET count = count+1 WHERE entry_id='$control'"); } $url = $_SERVER['PHP_SELF'].'?'.$url; header("Location:$url"); mysql_close($conn); ?> Code (markup): but if you only use id as http variable you dont need any modification in your script your script will work.