954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySql jump / click counter script help needed from a Newbie

I'm real stuck on a php jump script.
Basically a value is called from my .html pages; somthing like this: which looks at the jump.php page and redirects the user.

I track the number of clicks in a MySql database. I want the code to check the database to see if a link already exists. If it does, I want to add 1 to the click count. If it doesn't, I want to be create a new entry. That's all.

I've tested each section of the code and each bit works. Put it toguther and for some reason, it fails and I end up with loads of duplicate entries. My if condition may be incorrect. My php / mySql knowledge isn't great as I've only just started, so please bear with me!


//Here's my code - left out the connection part.
if ($m == "a")
{$link = " <a href="http://www.web1.html">http://www.web1.html</a> ";}
if ($m == "b")
{$link = " <a href="http://www.web2.html">http://www.web2.html</a> ";}
if ($m == "c")
{$link = " <a href="http://www.web3.html">http://www.web3.html</a> ";}

// Check if the $m value exists in the db
$result = mysql_query("SELECT name FROM clicks WHERE name='$m'") or die(mysql_error());
$row = mysql_fetch_array($result);

if ($row['name'] == '$m') //Look for $m in the database and see if it exists.

{
$result = mysql_query("UPDATE clicks SET clicks=clicks WHERE name='$m'") or die(mysql_error());
}

else
{
$result = mysql_query("INSERT INTO clicks (name,links) VALUES ('$m','somelinks')") or die(mysql_error());
}
header("Location: $link"); // Jump to the hiddden affiliate URL above
exit();
?>


Any ideas, clever people!
Mork

monkey64
Newbie Poster
1 post since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

first, you can put those if statements into a case
[php]
switch ($m) {
case 'a':
$link = 'web1';
break;
case 'b':
$link = 'web2';
break;
case 'c':
$link = 'web3';
break;
default:
$link = 'default';
break;
}
$link = 'http://www.' . $link . '.html';
[/php]
That will improve readability, and you can add or subtract fields w/o going crazy in if statements.
Then, you need to test your sql query to see if you have gotten any results back.
[php]
$result = mysql_query("SELECT name FROM clicks WHERE name='".$m."'");
if (mysql_num_rows($result)) {
$row = mysql_fetch_array($result);
} else {
die('there were no fields returned by this query');
}
[/php]
This will only drop into the mysql_fetch_array if there are not zero rows returned. Also note that you were wrapping your $m inside of single quotes which are treated as literal, so every time you were searching fr $m and not the variable you had in mind. Look at how I cahnged your query in my example.

sn4rf3r
Junior Poster
135 posts since Sep 2006
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You