I see many things wrong with this simple piece of code.
Watch the magic unfold.
code:
<?php
//if(isset($_COOKIE['user_ip'])) $set = true; //completly unnessecary. And a potential bug, too
//repeated code should be written only once.
function error($strMessage)
{
print $strMessage. ': ';
print mysql_error(). ' - '. mysql_error();
die();
}
//common code should be common, not repetead in every instance.
$link = mysql_connect($db_uri, $user_name, $pass) or error("Could not connect"); //using the new error function.
mysql_select_db($user_name) or error("Could not select database"); //and again. We already saved something.
//if(!$set) //what happens if the user appends ?set=true to the url.
//we'll use this instead
if (isset($_COOKIES['user_ip']))
{
setcookie("user_ip", $REMOTE_ADDR, time()+360000); //I'd add a path and a domain.
$iCountID = 1; //we'll be using this magic value twice, so we pull it into a variable.
mysql_query("UPDATE simplecount SET count=(count + 1) WHERE count_id={$iCountID}") or error("update query failed");
//$inker++; //now, where does $inker come from. What's it's initial value that you're incrementing.
$strQuery = "SELECT count FROM simplecount WHERE count_id={$iCountID}";
$hRes = mysql_query($strQuery);
if ($hRes)
{
$arrRow = mysql_fetch_array($hRes);
if ($arrRow)
{
$inker = $arrRow['count'];
}
else
{
$inker = 0;
}
}
else
{
error("select query failed: $strQuery");
}
$whackerror = "this is an error used to help report bugs. please ignore it. - $inker"; //printing this might be a good idea.
}
//no else needed, since the case was now empty
?>
Now it looks better, and will be easier to extend.
Your actually might have wanted to set $inker = 0 at the beginning instead of pulling it out of the database, but you don't have any loops in there, it won't ever be run twice without calling the page twice (which might just be what's happening. Further investigation is needed).
So long,
->Tyberius Prime
Edit: With a closing [ code], it looks much better...
(Edited by Tyberius Prime on 06-17-2004 19:42)