Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating a score (+1) in mysql table everytime an image is clicked (<a>). Using php
    text
    copied!<p>I'm working on a website just now, and basically it has 2 images next to each other.</p> <p>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.</p> <p>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.</p> <p>I am only wanting the score for the one the user clicks to update by 1 in the db.</p> <p>Is it because it is located within the tag?</p> <p>Here is the code, and any help would be greatly appreciated.</p> <pre><code>&lt;?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]."&amp;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 = "&lt;img src=\"{$result_cat1['path']}\" class=\"content_image\" alt=\"{$result_cat1['description']}\" title=\"{$result_cat1['description']}\" /&gt;&lt;br /&gt;"; $image_cat1 = "&lt;img src=\"{$result_cat2['path']}\" class=\"content_image\" alt=\"{$result_cat2['description']}\" title=\"{$result_cat2['description']}\" /&gt;&lt;br /&gt;"; $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}&amp;cat2={$random_cat2}"; ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;?php echo $pageTitle; ?&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;link rel="stylesheet" media="screen" href="css/style.css" type="text/css" /&gt; &lt;link rel="shortcut icon" href="images/favicon.ico" /&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="wrapper"&gt; &lt;div id="navigation"&gt; &lt;a href="index.php"&gt;&lt;div id="home_icon" alth="Home Icon" title="Home"&gt;&lt;/div&gt;&lt;/a&gt; &lt;a href="&lt;?php echo $random_btn; ?&gt;"&gt;&lt;div id="random_icon" alth="Random Icon" title="Random"&gt;&lt;/div&gt;&lt;/a&gt; &lt;div id="image_title"&gt; &lt;p&gt;TITLE&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="content"&gt; &lt;!-- Content [Image] cat1 --&gt; &lt;a href="&lt;?php mysql_query("UPDATE `table1` SET `score` = `score` + 1 WHERE `id` = $id_cat1"); echo $random_btn; ?&gt;"&gt;&lt;?php echo $image_cat1; ?&gt;&lt;/a&gt;&lt;br/&gt; &lt;!-- Content [Image] cat2 --&gt; &lt;a href="&lt;?php mysql_query("UPDATE `table2` SET `score` = `score` + 1 WHERE `id` = $id_cat2"); echo $random_btn; ?&gt;"&gt;&lt;?php echo $image_cat2; ?&gt;&lt;/a&gt;&lt;br/&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload