Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX -> PHP not updating MySQL database consistently
    primarykey
    data
    text
    <p>So this is my early attempt at a Facemash style site in which the user will select one of two images, scoring a hit with the chosen image (the winner) and a miss with the unselected image (the loser) - both of which are recorded in a MySQL database.</p> <p>The selected image is determined using javascript and uses jquery AJAX to notify a PHP script (backend.php) which updates the database.</p> <p>This works absolutely correctly for updating the "hits" field. However, the "misses" are not consistently recorded. By this I mean that when the user clicks one image, the fact the other image has not been clicked is only <em>sometimes</em> shown in the database. As far as I can tell there is no pattern as to when the "miss" is and is not recorded, making it difficult to pinpoint where the problem lies.</p> <p>I've checked the code over and over again and cannot understand why this is happening or what would be responsible for it, so I thought it would be best to post everything. I appreciate it's a lot to ask, but any explaination as to why I'm having this problem would be hugely appreciated, thanks.</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Facemash&lt;/title&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;?php // Make a MySQL Connection mysql_connect("localhost", "admin", "admin") or die(mysql_error()); mysql_select_db("facemash") or die(mysql_error()); // Select two random people $personA = rand(1, 28); $personB = rand(1, 28); // Ensure that it is not the same person if ($personB == $personA) { $personB = rand(1, 28); } // Function to return path of photo function photoPath ($person){ $query = mysql_query("SELECT photo FROM people WHERE id=$person"); $result = mysql_fetch_row($query); $result = $result[0]; echo $result; } ?&gt; &lt;!--Image for personA--&gt; &lt;div id=photoA identity="&lt;?php echo $personA ?&gt;"&gt;&lt;img src="&lt;?php photoPath($personA);?&gt;"/&gt;&lt;/div&gt; &lt;!--Image for personB--&gt; &lt;div id=photoB identity="&lt;?php echo $personB ?&gt;"&gt;&lt;img src="&lt;?php photoPath($personB);?&gt;"/&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; $('#photoA').click(function() { var hit = $('#photoA[identity]').attr('identity'); var miss = $('#photoB[identity]').attr('identity'); $.post ("backend.php", {winner: hit} ); $.post ("backend.php", {loser: miss} ); location.reload(true); }); $('#photoB').click(function() { var hit = $('#photoB[identity]').attr('identity'); var miss = $('#photoA[identity]').attr('identity'); $.post ("backend.php", {winner: hit} ); $.post ("backend.php", {loser: miss} ); location.reload(true); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>backend.php:</p> <pre><code>&lt;?php // Make a MySQL Connection mysql_connect("localhost", "admin", "admin") or die(mysql_error()); mysql_select_db("facemash") or die(mysql_error()); // Recieve id of winner from index.php $winner = $_POST['winner']; // Recieve id of loser from index.php $loser = $_POST['loser']; // Lookup hits for winner and update by adding 1 function updateHits ($winner) { $query = mysql_query("SELECT hits FROM people WHERE id=$winner"); $result = mysql_fetch_row($query); $result = $result[0]; $result++; mysql_query("UPDATE people SET hits = $result WHERE id=$winner"); } //Lookup misses for loser and update by adding 1 function updateMisses ($loser) { $query = mysql_query("SELECT misses FROM people WHERE id=$loser"); $result = mysql_fetch_row($query); $result = $result[0]; $result++; mysql_query("UPDATE people SET misses = $result WHERE id=$loser"); } updateHits($winner); updateMisses($loser); ?&gt; </code></pre> <p>Thanks again.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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