Forum Moderators: coopster

Message Too Old, No Replies

Click counter (PHP&MySQL) not working

         

Lupi

3:17 pm on Aug 8, 2006 (gmt 0)

10+ Year Member



Hi,

I'm trying to create a simple click counter for my website (PHP&MySQL) using FiRe's tutorial: [webmasterworld.com...]

I tried to modify it a bit, since I want links like

www.mysite.com/buy.php?at=merchant.com

I used PHPMyAdmin to create a table called "buy" with three fields:
at varchar(50) (primary key)
url text
clicks int(10)

I put in some sample data:
at merchantABC.com
url [merchantABC.com...]
clicks 0

Then, I created the buy.php script:

=================================

<?php

//Connect
$dbhandle = mysql_connect("localhost","dbusername","dbpassword")
or die ("Inactive.");

//Get ID
$id = $_GET['id'];

//Update clicks
mysql_query("UPDATE buy SET clicks=clicks+1 WHERE id='$id'");

//Retrieve URL
$sql = mysql_query("SELECT url FROM buy WHERE id='$id'");
$fetch = mysql_fetch_row($sql);
$url = $fetch[1];

mysql_close();

//Redirect to URL
header ("Location: $url");

?>

=================================

I put a link to buy.php?at=merchantABC.com on my website.

When I clicked on it, I received the following error messages:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/www/buy.php on line 15

Warning: Cannot modify header information - headers already sent by (output started at /home/www/buy.php:15) in /home/www/buy.php on line 21

I'm very new to creating dynamic sites, so if anyone could have a look at my code, I'd appreciate it.

Thanks.

Have a good day,
Lupi

dreamcatcher

3:21 pm on Aug 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Lupi,

Your $id variable has no value, which is the reason your query is failing and you are getting the SQL error:

Shouldn`t this line:

buy.php?at=merchantABC.com

actually be:

buy.php?id=merchantABC.com

The variable you are creating at the moment is going to be $_GET['at'], and not $_GET['id'].

dc

Lupi

1:06 pm on Aug 9, 2006 (gmt 0)

10+ Year Member



Thank you, dreamcatcher.

Apparently, the discrepancy you pointed out was only part of the problem.

<?php
//Connect
$dbhandle = mysql_connect("localhost","w******","d******") or die ("inactive");
mysql_select_db('w******', $dbhandle) or die ("inactive");
//Get ID
$at = $_GET['at'];
//Update clicks
mysql_query("UPDATE buy SET clicks=clicks+1 WHERE at='$at'");
//Retrieve URL
$sql = mysql_query("SELECT url FROM buy WHERE at='$at'");
$fetch = mysql_fetch_row($sql);
$url = $fetch;
mysql_close($dbhandle);
//Redirect to URL
header ("Location: $url");
exit;
?>

On my main page (index.html), a have a link to
buy.php?at=merchantABC

If I click on it, I don't get any error messages, but the redirect doesn't work, I just get the blank page (buy.php?at=merchantABC).

Any ideas?

- Lupi

[1][edited by: Lupi at 1:23 pm (utc) on Aug. 9, 2006]

FiRe

1:09 pm on Aug 9, 2006 (gmt 0)

10+ Year Member



$url = $fetch[1];

should be

$url = $fetch[0];

;-)

Lupi

1:24 pm on Aug 9, 2006 (gmt 0)

10+ Year Member



That works, perfect. Thanks a lot for the help, FiRe and dreamcatcher!

dreamcatcher

1:35 pm on Aug 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it sorted Lupi. mysql_fetch_row returns the result set as a numerically indiced array as pointed out by FiRe.

dc