Jump to content

Code Not Working. Want to Count Clicks and Redirect Visitors


Fluoresce

Recommended Posts

Can you see any problems with the code below? It doesn't work.

 

What I am trying to do is quite straightforward.

 

The links on my site look like this:

 

go.php?id=1&url=usa

go.php?id=1&url=uk

go.php?id=2&url=usa

go.php?id=2&url=uk

 

When a visitor clicks on one of these links, I want to grab the values of the id and url parameters, count the click, and redirect the visitor.

 

Here's the error that I get when I try the code:

 

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\sites\go.php on line 18

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\sites\go.php:18) in C:\xampp\htdocs\sites\go.php on line 19

 

<?php
$conn = mysql_connect('localhost','username', 'password') or trigger_error("SQL", E_USER_ERROR);
mysql_select_db('database', $conn) or trigger_error("SQL", E_USER_ERROR);
$id = intval($_GET['id']);
$url = intval($_GET['url']);
mysql_query("UPDATE urls SET click_counter = click_counter+1 WHERE id=$id");
if ($url=="usa") 
	$href = "SELECT usa FROM urls WHERE id=$id";				
elseif ($url=="uk")
	$href = "SELECT uk FROM urls WHERE id=$id";
elseif ($url=="aus")
	$href = "SELECT aus FROM urls WHERE id=$id";
elseif ($url=="can")
	$href = "SELECT can FROM urls WHERE id=$id";
else
	$href = "SELECT int FROM urls WHERE id=$id";	
$qry = mysql_query($href);
list($href)=mysql_fetch_row($qry);
header("Location:$href");
mysql_close($conn);
?>

Umm, seems your sql query is failing change this

$qry = mysql_query($href);

to

$qry = mysql_query($href) or trigger_error(mysql_error());

 

Also you'll be better to code this

	if ($url=="usa") 
	$href = "SELECT usa FROM urls WHERE id=$id";				
elseif ($url=="uk")
	$href = "SELECT uk FROM urls WHERE id=$id";
elseif ($url=="aus")
	$href = "SELECT aus FROM urls WHERE id=$id";
elseif ($url=="can")
	$href = "SELECT can FROM urls WHERE id=$id";
else
	$href = "SELECT int FROM urls WHERE id=$id";	

 

As a switch/case statement

    switch(strtolower($url))
    {
        case 'uk':
        case 'usa':
        case 'aus':
        case 'can':
            $field = $url;
        break;
        
        case' int':
        default:
            $field = 'int';
        break;
    }	
    
    $href = "SELECT $field FROM urls WHERE id=$id";

 

Also this

	$url = intval($_GET['url']);

Should be

	$url = $_GET['url'];

Thanks, Wildteen88!

 

It nearly works. It only fails when a link with the url parameter "int" is clicked (e.g., go.php?id=1&url=int). Here's the error message that I get:

 

Notice: 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 'int FROM urls WHERE id=1' at line 1 in C:\xampp\htdocs\sites\go.php on line 21

 

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\sites\go.php on line 22

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\sites\go.php:21) in C:\xampp\htdocs\sites\go.php on line 23

It is erroring out due to 'int' being a mysql reserved keyword. To prevent the error . Change

    $href = "SELECT $field FROM urls WHERE id=$id";

to

    $href = "SELECT `$field` FROM urls WHERE id=$id";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.