Forum Moderators: coopster

Message Too Old, No Replies

Count click help

count link

         

chrissim

6:42 am on May 28, 2010 (gmt 0)

10+ Year Member



hi,

i have an existing table with the following field like id, website, hits, click and date. Everything were working well with all the data recorded in my referrer script except the 'click' counter field that does not even increment any count and redirect the link from my out.php script below when i tried to click on the ongoing like http://www.example.com/out.php?id=10814.
i just cannot figured out what has gone wrong with these code


out.php


<?

$id=$_GET['id'];

//Adds to the click count for a particular link
mysql_query("UPDATE uniquehits SET click = click +1 WHERE id = '$id'");


//Retrieves information
$dat = mysql_query("SELECT * FROM uniquehits WHERE id = '$id'") or die(mysql_error());
$r = mysql_fetch_array($data);

//redirects them to the link they clicked
header('Location: '".$r['website'] . "');

?>

[edited by: dreamcatcher at 7:08 am (utc) on May 28, 2010]
[edit reason] Use example.com, thank you [/edit]

Matthew1980

7:10 am on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there chrissim,

Firstly, even though the query is relatively simple, I would build it outside of the query function, purely so that you can echo it to screen for debug similar to this:-
$SqlQuery = "UPDATE `uniquehits` SET `click` = 'click+1' WHERE id = '".$id."' ";
echo $SqlQuery;
exit;//Just add this to kill the script there so you can see whats what

mysql_query($SqlQuery, $connectionRef) or die(mysql_error());

Same for the retrieval (typo in the var!):-
$SqlQuery1 = "SELECT * FROM `uniquehits` WHERE `id` = '".$id."'";
echo $SqlQuery1;
exit; Just to check the how the statement is populated


$data = mysql_query($SqlQuery1) or die(mysql_error());

$r = mysql_fetch_array($data);
//I assume that there is only ever going to be 1 result returned.

Other than the typo ($data) the only other thing I wonder about is the value in the field where you are inrementing - surely its just an int (+1) and not char?

Hope that helps ;-)

[EDIT]: DC; there must have been microseconds to that ;) lol!

Cheers,
MRb

[edited by: Matthew1980 at 7:16 am (utc) on May 28, 2010]

dreamcatcher

7:10 am on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi chrissim,

You have a typo in your code:

$dat = mysql_query...

should be:

$data = mysql_query...

dc

[edit - Matthew beat me to it.]

chrissim

7:36 am on May 28, 2010 (gmt 0)

10+ Year Member



hi Matthew1980

Yes the debug shown on my screen as UPDATE `uniquehits` SET `click` = 'click+1' WHERE id = '10814' but it does not update the count to 1 at all. The click field was create as click INT(11) UNSIGNED NOT NULL DEFAULT 0
The click counter of the specific id '10814' still remain 0 and it doesn't incrementing at all.

Please help

Matthew1980

8:13 am on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Chrissim,

As me & dc pointed out, did you correct the var? It will never update if you don't change that.

Also for server portability use the full tags - lots of servers don't support that now ;-) <?php?>

As you are developing too, put the error_reporting on: error_reporting(E_ALL); this will then flag up any errors you may have.

So the data conatined in the field is just an int then? if so just put the sql to reflect that:-

... SET `click` = '+1' WHERE `id` ... etc, etc

as it's just an int you only need to +1 to it, so there is no need to put click+1 - please correct me if I am wrong, thats just how I see that, there may be other ways to do that, there usually is ;)

Cheers,
MRb

chrissim

2:11 am on May 29, 2010 (gmt 0)

10+ Year Member



hi Matthew1980,

ok i got them count increment with no problem now but i have some problem of redirect them to the specific url that link to the actual id. It kept on redirect them back to my own index home page.
Here's the code



//Retrieves information
$sql = "SELECT website FROM uniquehits WHERE id=$id";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($query);
$website = $row["website"];

//redirects them to the link they clicked
header( "Location:" .$row['website'] );

rocknbil

3:42 am on May 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you do this

$website = $row["website"];

no real need to do this

header("Location:" .$row['website'] );

and it's likely because you need a closing quote on the header function after $row['website']. but no matter, change it to

header("Location:$website");

and if that doesn't work, find out why with

echo "Location:$website";
exit;

chrissim

4:11 am on May 29, 2010 (gmt 0)

10+ Year Member



hi rocknbill

i tried both the suggestion and it still redirect them back to my site. i tried echoing the result of the Location which are the correct domain that should be direct. But still it doesn't direct them to the correctly. Any suggestion to my problem here?

dreamcatcher

5:43 am on May 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check that you don`t have any whitespace in the PHP file before or after the opening PHP tag or closing PHP tag. You might want to check your server error log as well to see if something has been logged there that is causing this to fail.

Also, you don`t have any other header functions in place do you? Could it be actually directing to the correct page, but then another check is directing the person to another page?

dc

Furiat

7:33 am on May 29, 2010 (gmt 0)

10+ Year Member



In regards to dreamcatcher's post: Also if you're using UTF-8 encoding for the php files make sure it's UTF-8 without BOM (Byte Order Mark).

chrissim

2:22 pm on May 29, 2010 (gmt 0)

10+ Year Member



Thanks guys for all your support and assistance to my problem. i've got the scripts done finally. :)