I'm real stuck on a php jump script.
Basically a value is called from my .html pages; somthing like this: <a href="jump.php?m=c"> 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 = "[URL]http://www.web1.html[/URL]";}
if ($m == "b")
{$link = "[URL]http://www.web2.html[/URL]";}
if ($m == "c")
{$link = "[URL]http://www.web3.html[/URL]";}
 
// 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

first, you can put those if statements into a case

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';

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.

$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');
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.