tags:

views:

47

answers:

4

I have a count field in a table that I need to increment and this is what I have.

$click_tracker_row = mysql_fetch_array($result);
$current_count = $click_tracker_row['count'];
$new_count = $current_count + 1;
$query_wiki ="UPDATE click_tracker  SET count = '{$new_count}' WHERE click_tracker_id = '{$click_tracker_row['click_tracker_id']}' LIMIT 1";                 
$result = mysql_query($query) ;

But it never changes....is there a better way of doing this and why is this not working count is an integer field

+3  A: 

To address the first question is there a better way of doing this, yes. For incrementing a count in MySQL, you do not need to fetch anything as long as you have the id to update and you should also make sure the id is properly escaped for the query.

$id = mysql_real_escape_string($id); // replace $id with however you get the tracker_id
$query_wiki ="UPDATE click_tracker  SET count = count + 1 WHERE click_tracker_id = '{$id}' LIMIT 1"; 

To answer the second question, why is this not working in the lower section you are doing a mysql_query($query) instead of a mysql_query($query_wiki).

Brad F Jacobs
A: 
$click_tracker_row = mysql_fetch_assoc($result);
$query_wiki ="UPDATE `click_tracker`
              SET `count` = `count` + 1 
              WHERE `click_tracker_id` = '".$click_tracker_row['click_tracker_id']."'         
              LIMIT 1";                 
$result = mysql_query($$query_wiki) ;
Alexander.Plutov
+6  A: 

You should simply update this counter using SQL and use PHP's mysql_real_escape_string()-function to prevent SQL injection:

$query_wiki ="UPDATE click_tracker SET count = count + 1 WHERE click_tracker_id = '".mysql_real_escape_string($click_tracker_row['click_tracker_id'])."' LIMIT 1";

Furthermore, there is a typo in your mysql_query()-call. You will need to pass $query_wiki to it.

elusive
Given the data is coming from a trusted source (the MySQL database) I would not think you would need to escape the data in this scenario. But there in lies the issue, he is querying the database twice when a single query would suffice, if he already has the id (which he must) so that variable, I would agree needs to be escaped.
Brad F Jacobs
I added this hint, since we cannot be sure wether this is from a trusted source. My normal approach is to make sure that it is really safe. SQL injection is no fun. I would recommend using something like PDO instead of PHP's `mysql_*`-functions.
elusive
Not only the typo, but when did `mysql_fetch_array` fetch associatively ?
RobertPitt
@RobertPitt: Since always. It fetches both, assoc and number based. So it is better to use it with `MYSQL_ASSOC` as it defaults to `MYSQL_BOTH` or to just use the `mysql_fetch_assoc` since that is what you want anyways, as not specifying basically doubles the results. See http://www.php.net/mysql_fetch_array on what it returns and its usage.
Brad F Jacobs
ooh then i sadly withdraw my comment, im so used to using assoc over array for the fact it returns half the amount of rows. +1 for you anyways.
RobertPitt
A: 

If you have a lot of clicks then incrementing the count with each click can become inefficient because it causes a ton of small database updates. In other words, you want to do this

UPDATE click_tracker SET count = count + 5...

instead of

UPDATE click_tracker SET count = count + 1...
UPDATE click_tracker SET count = count + 1...
UPDATE click_tracker SET count = count + 1...
UPDATE click_tracker SET count = count + 1...
UPDATE click_tracker SET count = count + 1...

Here's an article describing the idea. If you google php rabbitmq you can get an idea for how to build a php version.

Dave Aaron Smith