update "hit/click" SET + 1 counter - code not working

darrenhallinan 22 Feb, 2012
Hello everyone

I have this bit of code I need a little help with

<?php
$uid = $_GET['url_id'];
$db =& JFactory::getDBO();
$query = "
UPDATE `#__chronoforms_data_competitions`
SET `clicks` = '( clicks + 1 )'
WHERE `cf_uid` = '$uid' ;
";
$db->setQuery($query);
$db->query();
?>


Thats the code. . .

here is what i have:

I am using CC V4 and have a form to hold competitions

I wish to show how many people have entered the competitions

so in the table i have included a column called "clicks"

on the page I have the above code the cf_uid is within the url as url_id

the clicks column is set exactly the same as the hits in jos_content
table: content
hits int(10) UNSIGNED No 0
table: chronoforms_data_competitions
clicks int(10) UNSIGNED No 0

the code does work to the extent of making a connection but it does not increase the value in the column it actually resets it to "0" as I have gone into my php admin and give it a value of say 22 and then refreshed the page with the above code and looked and the value went back to zero.

so the issue must be with this part of the code:
SET `clicks` = '( clicks + 1 )'


any one know where I am going wrong?

thanks in advance
nml375 22 Feb, 2012
Hi darrenhallinan,
The first thing that comes to mind, is that you are handling the clicks field as a string, rather than an integer. This causes MySQL to convert the literal string (clicks + 1) into an integer (which is in fact 0), rather than extract the value of the field clicks and add 1.

Modify your query like below;
SET `clicks` = `clicks` + 1


/Fredrik
darrenhallinan 23 Feb, 2012
Hi Fredrik

thanks for the reply

when i change it to

SET `clicks` = `clicks` + 1


it just makes the value "1"

this is the setup of the clicks column
[attachment=0]mysql_clicks.jpg[/attachment]

I think its all set ok - but obviously im missing something as it does not work any other ideas?
darrenhallinan 23 Feb, 2012
solved - my fault i had the old code in a custom code box in the wizard that was messing with it

thanks again for your help
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger