Pass ID var in URL to second page then Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
onalaska98
Forum Newbie
Posts: 5
Joined: Wed Jun 08, 2005 9:11 pm

Pass ID var in URL to second page then Error

Post by onalaska98 »

I have two pages. The first one has links to other web sites. When the link is clicked it links to the second page with a "?URLID=#" in the url. i.e. - http://www.domain.com/secondpage.php?URLID=2

On the second page I take the URLID and look it up in a mysql db to update the clickcount field and retrieve the real URL and then I "header(location:)" them to the correct site. I keep getting an error of:

"You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

I think this is inaccurate as line one is "<?php" Also if I change my $URLID var in the

Code: Select all

$result = mysql_query("SELECT * FROM linkcount WHERE ID = $URLID") or die(mysql_error());
line to a static '1' or other ID it works fine.[/b] It just doesn't seem to like the variable in there. Can anyone help? Requested stats and full code below:

PHP Version: 4.3.11
Display Errors: Off
Error Level: E_ALL
Register Globals: Off

Code: Select all

<?php
//* declare connection variables */
$DBhost = "localhost";
$DBuser = "user";
$DBpass = "password";
$DBName = "dbname";
$table = "linkcount";

$conn = mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable toconnect to database");
if (!$conn) {
	die('Could Not Connect: ' . mysql_error());
}
//echo 'Connected Successfully';

mysql_select_db("$DBName") or die("Unable to select database $DBName");

$result = mysql_query("SELECT * FROM linkcount WHERE ID = $URLID") or die(mysql_error());

//get results from first and hopefully only, entry
while ($row = mysql_fetch_array($result)) {
	$tmpCount = $row['HitCount'] + 1;
	mysql_query("UPDATE linkcount SET HitCount =  ".$tmpCount." WHERE ID = ".$row['ID']) or die(mysql_error());
	header("Location: http://" . $row['URL']);
}


//mysql_close();
?>
If anyone can tell me where I am going wrong, I would sure appreciate it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$result = mysql_query("SELECT * FROM `linkcount` WHERE `ID` = '".$_GET['URLID']."'") or die(mysql_error());
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try setting URLID before your query:

Code: Select all

$URLID = $_GET['URLID'];
onalaska98
Forum Newbie
Posts: 5
Joined: Wed Jun 08, 2005 9:11 pm

Post by onalaska98 »

Jcart wrote:

Code: Select all

$result = mysql_query("SELECT * FROM `linkcount` WHERE `ID` = '".$_GET['URLID']."'") or die(mysql_error());
Thank you Jcart that worked perfect. Sorry for being a n00b!

Burrito I appreciate you taking the time to help as well!
Post Reply