I'm trying to create an outgoing link click counter. Found some code online and can't understand for the life of me why it does not update the number within the database. What am I doing wrong here?
<?php
$linkid = $_GET["id"];
mysql_query("UPDATE research SET out = out + 1 WHERE id='$linkid'");
$query = "SELECT * FROM research WHERE id='$linkid'";
$result = mysql_query( $query ) or die ("Error in query: $query. ".mysql_error());
while($row = mysql_fetch_row( $result ) ) {
header ("Location:" .$row[2] );
}
?>
Your best bet to understand how this code is working is to learn to check the data like this:
//connect to db here before the rest of your code
if(isset($_GET["id"]){ //only execute if GET is set
$linkid = $_GET["id"];
echo 'GET = '.$linkid.' <br/>'; //check the value to check against your database for testing
mysql_query("UPDATE research SET out= out+1 WHERE id='$linkid'") or die(mysql_error());
//or die helps detect syntax mistakes
if(mysql_affected_rows()){ //if update did occur
$query = "SELECT fieldname FROM research WHERE id='$linkid'";
//no need to use * just select the on fields you need!
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if(mysql_num_rows($result)>0){ //if a row is found with that id
$row = mysql_fetch_assoc();
echo 'Field Value = '.$row['fieldname'];
//header("location:".$row['fieldname']); - temporarily commented out as headers already sent
} else { echo 'id does not exist in research table'; }
} else { echo 'update did not occur'; }
} else { echo 'GET not set!'; }
I see nothing wrong with your syntax but a few checkers can help explain why it might not be working!
With my script check the outputs and compare it to your database (be sure to check fieldname to the actual name of the field!
It should highlight why it's not working. I've added comments to explain what is going on encase your unfamiliar with some of the function names that i used.