Forum Moderators: coopster
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
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
Apparently, the discrepancy you pointed out was only part of the problem.
<?php On my main page (index.html), a have a link to 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]
//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;
?>
buy.php?at=merchantABC