I have the following ocde in my index.php page of my site.
The idea is to count the hits to the site from specific url variables... for example:
http://www.website.com/id=some%20variable
Here is the code that I am using:
<?
//Database user info
$username="l";
$password="";
$database="";
//Connect to database
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
//print errors if any
echo mysql_error();
//make a query to find out, if the url is already in the database
$sql=mysql_query("SELECT times FROM who WHERE url='$id'");
if (!$sql) echo "didn't work";
$count=mysql_num_rows($sql);
if ($count<1){
//if not, insert a new row with the values from the variable id and 1 (because it's the first time to insert this url)
$sql=mysql_query("INSERT INTO who (url, times) VALUES ('$id', '1')");
if (!$sql) echo "didn't work";
}
//if this url is already listed in the db, just update it, making the 'times' one more
else{
$x=$query[1]+1;
$query=mysql_query("UPDATE who SET times='$x' WHERE url='$id'");
if (!$sql) echo "didn't work";
}
//disconnect from the database
mysql_close();
?>
The problem that I am having is that if a url is not currently recorded in the database, it will insert it and update times to 1.
If it is already in the database it does not increase the count of instances.
Can anyone show me what I have done wrong, or if I have left something out?
Thanks in advance.