Reputation: 71
I'm working on a website just now, and basically it has 2 images next to each other.
What I want to happen is when you click one, and not the other, it will update the score in the database +1 for the photo you clicked.
Just now both pictures seem to be getting their score in their respective tables updated by 1, but both on page load which is annoying.
I am only wanting the score for the one the user clicks to update by 1 in the db.
Is it because it is located within the tag?
Here is the code, and any help would be greatly appreciated.
<?php
//Connect to database
mysql_connect("localhost","username","password")
or die("Could not connect: ".mysql_error());
mysql_select_db("database")
or die("Could not connect to database, ".mysql_error());
//Variables
$thisPage = "index.php";
$count_query_cat1= mysql_query("SELECT COUNT(*) FROM `cat1`");
$count_query_cat2= mysql_query("SELECT COUNT(*) FROM `cat2`");
$count_num_cat1 = mysql_fetch_array($count_query_cat1);
$count_num_cat2 = mysql_fetch_array($count_query_cat2);
if (isset($_GET['cat1']))
{
$id_cat1 = (int) $_GET['cat1'];
}
if (isset($_GET['cat2']))
{
$id_cat2 = (int) $_GET['cat2'];
}
if ($id_cat1 == 0)
{
$url = $thisPage."?cat1=".$count_num_cat1[0]."&cat2=".$count_num_cat2[0];
header("Location: $url");
}
$query_cat1 = mysql_query("SELECT * FROM `table1` WHERE `id` = $id_cat1");
$result_cat1 = mysql_fetch_assoc($query_cat1);
$query_cat2 = mysql_query("SELECT * FROM `table2` WHERE `id` = $id_cat2");
$result_cat2 = mysql_fetch_assoc($query_cat2);
$image_cat1 = "<img src=\"{$result_cat1['path']}\" class=\"content_image\" alt=\"{$result_cat1['description']}\" title=\"{$result_cat1['description']}\" /><br />";
$image_cat1 = "<img src=\"{$result_cat2['path']}\" class=\"content_image\" alt=\"{$result_cat2['description']}\" title=\"{$result_cat2['description']}\" /><br />";
$description_cat1 = $result_cat1['description'];
$description_cat2 = $result_cat2['description'];
//Random button
$random_cat1 = rand(1,$count_num_cat1[0]);
if ($random_cat1 == $count_num_cat1[0]){
$random_cat1 = rand(1,$count_num_cat1[0]);
}
$random_cat2 = rand(1,$count_num_cat2[0]);
if ($random_cat2 == $count_num_cat2[0]){
$random_cat2 = rand(1,$count_num_cat2[0]);
}
$random_btn = "{$thisPage}?cat1={$random_cat1}&cat2={$random_cat2}";
?>
<!DOCTYPE html>
<html>
<head>
<?php echo $pageTitle; ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" media="screen" href="css/style.css" type="text/css" />
<link rel="shortcut icon" href="images/favicon.ico" />
</head>
<body>
<div id="wrapper">
<div id="navigation">
<a href="index.php"><div id="home_icon" alth="Home Icon" title="Home"></div></a>
<a href="<?php echo $random_btn; ?>"><div id="random_icon" alth="Random Icon" title="Random"></div></a>
<div id="image_title">
<p>TITLE</p>
</div>
</div>
<div id="content">
<!-- Content [Image] cat1 -->
<a href="<?php mysql_query("UPDATE `table1` SET `score` = `score` + 1 WHERE `id` = $id_cat1"); echo $random_btn; ?>"><?php echo $image_cat1; ?></a><br/>
<!-- Content [Image] cat2 -->
<a href="<?php mysql_query("UPDATE `table2` SET `score` = `score` + 1 WHERE `id` = $id_cat2"); echo $random_btn; ?>"><?php echo $image_cat2; ?></a><br/>
</div>
</body>
</html>
Upvotes: 1
Views: 1532
Reputation: 733
PHP code gets executed on page load, unless stated otherwise (by some IF statement or such). So, if you put any kind of PHP code inside your HTML tag, like here:
<a href="<?php mysql_query("UPDATE `table2` SET `score` = `score` + 1 WHERE `id` = $id_cat2"); echo $random_btn; ?>"><?php echo $image_cat2; ?></a><br/>
code you wrote inside of href attribute gets executed, no matter whether the link is clicked or not.
Solution is as following: In href attribute, write "?update_photo=1", and in the ohter anchor write href="?update_photo=2".
Inside the PHP tags write the following:
if(isset($_GET['update_photo'])){
$id = (int)$_GET['update_photo'];
mysql_query("UPDATE `table2` SET `score` = `score` + 1 WHERE `id` = {$id}");
}
Upvotes: 1