<?php
// Quote variable to make safe
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not a number or a numeric string
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
if(isset($_GET['id'])){
mysql_select_db('**', mysql_pconnect('**','**','**')) or die (mysql_error());
$filtered_id = htmlentities($_GET[id]);
$id = quote_smart($filtered_id);
$filtered_id = htmlentities($_GET[id]);
$id = quote_smart($filtered_id);
if (ctype_digit($id)) { //id has been filtered
$query_id="SELECT * FROM tutorials WHERE id=$id";
$row_id=mysql_query($query_id);
$result_id=mysql_fetch_array($row_id);
$page_title="".$result_id['title']." - ".$result_id['category']." tutorial";
$hits=$result_id['hits']++;
}
}
include("./header.php");
mysql_select_db('**', mysql_pconnect('**','**','**')) or die (mysql_error());
if(ctype_digit($id)){
$query_hits="UPDATE tutorials SET hits=$hits WHERE id=$id";
mysql_query("$query_hits") or die ("Could not update hits!");
echo "<h1>".$result_id['title']."</h1>
<b>By: ".$result_id['author']."</b><br /><br />
".$result_id['content']."<br />$hits";
}
if(!ctype_digit($id)){
echo "Error: Tutorial doesn't exist!";
}
if(!isset($_GET['id'])){
echo "You must select a tutorial!<br /><br />";
}
?>
<br /><br />
<?php include("./footer.php"); ?>
That is the code I'm using. It's suppose to update the hits row of the tutorials table according to the id whenever the page is viewed. Unfortunately, this is not working at all, and the value of $hits is always 1 (even after I try to increase it), and the number of hits in the database always stays 1. The row just won't update, and I don't understand why. I tried something else, and the value of $hits was updated, but the databse just wouldn't update. I've tried many other things, but I just can't get it to work. >_> It seems to me that this should be easy, but I'm lost now. Any and all help would be greatly appreciated.
Thanks,
Daniel
P.S. I know quote_smart isn't necessary, but I'm using it for something else, so I'm just gonna leave it there. Also, I have to select the database twice as header.php connects to a different one.