Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counter does not increment in PHP/MySQL

Tags:

php

mysql

counter

I need to create a counter for member section (count the number of times a user logged).

I have the following script (counter.php):

<?php
    $conn = mysql_connect("localhost", "myuser", "mypass");
    mysql_select_db("test");

    $sql = "SELECT views FROM members WHERE mid = " . $_GET['mid'];     
    $result = mysql_query($sql); 
    if (!$result)
        {
        mail(ADMIN, 'Cannot Get: ' . mysql_error(), mysql_error());  
        }
    while ($row = mysql_fetch_assoc($result)) 
        {
        $count = $row['views']++; 
        }
    $query = "UPDATE members SET views = '$count' WHERE mid = " . $_GET['mid']; 
    mysql_query($query); 
    mysql_close($conn);

    // show the logo using header() and readfile(); // that part work
?>

DB:

CREATE TABLE `members` (
  `mid` int(11) NOT NULL AUTO_INCREMENT,
  `views` int(11) DEFAULT '0',
  /* etc...*/
  PRIMARY KEY (`mid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now, what I do in my .htaccess file is:

RewriteEngine On
RewriteRule ^img/logo([0-9]+).jpg$ /counter.php?mid=$1 [L]

but for some reason my counter does not count correctly. What am I missing?

like image 568
Tech4Wilco Avatar asked Oct 17 '11 18:10

Tech4Wilco


1 Answers

You could probably just simplify it and do the following:

$query = "UPDATE members SET views = views + 1 WHERE mid = " . $_GET['mid']; 
mysql_query($query); 

if (mysql_affected_rows() == 0) {
   mail(ADMIN, 'Cannot Get: ' . mysql_error(), mysql_error());
}

mysql_close($conn);

No need to do initial check.

like image 128
endyourif Avatar answered Oct 04 '22 12:10

endyourif