title: AJAX insert in MySQL delay - non inserting
body: <p>I'm programming kind of pool application - 
  I have about 50 divs - 1 shown, 49 hidden. In every div there is a form with radiobuttons and after user clicks on radiobutton, JS calls AJAX - PHP request and inserts answer into MySQL. After success, next div is shown and so on...</p>
  
  <p>Everything works fine, until i answer faster - about 3 question/second.</p>
  
  <p>Then some answers stop being stored. For exmaple answers 1,2,4,6,7,10 are stored. 3,5,8,9 are missing...</p>
  
  <p>How could i avoid it? It's really important to have consictency in answers.</p>
  
  <p>Here is my JS</p>
  
  <pre><code>$('input[type=radio]').click(function () {
  
          $.ajax({
              url: 'assets/ajax/save_answer.php',
              data: {action: 'save_answer', question: currentQuestion, answer: $('input[name=answer-' + currentQuestion + ']:checked').val(), answer_pattern: 'agree-disagree', seconds: currentTime},
              type: 'post',
              dataType: 'json',
              success: function (output) {
                  if (output.success) {
  
                      $('.loading img').css("display", "none");
  
  
                          $('#' + currentQuestion).remove();
                          currentQuestion += currentQuestion;
                          $('#' + currentQuestion).show();
  
  
                  } else {
  
                      alert("Answer wasn't stored");
                      $('.loading img').css("display", "none");
                  }
              },
              error: function () {
  
                  alert("Answer wasn't stored");
                  $('.loading img').css("display", "none");
              }
  
          });
  </code></pre>
  
  <p>PHP function</p>
  
  <pre><code>if (isset($_POST['action']) &amp;&amp; $_POST['action'] == "save_answer") {
  
  $resp = new stdClass();
  
  
  $config = HTMLPurifier_Config::createDefault();
  $purifier = new HTMLPurifier($config);
  
  
  $user = $purifier-&gt;purify($_SESSION['user_id']);
  $projekt = $purifier-&gt;purify($_SESSION['projekt_id']);
  $otazka = $purifier-&gt;purify($_POST['question']);
  $odpoved = $purifier-&gt;purify($_POST['answer']);
  $answer_pattern = $purifier-&gt;purify($_POST['answer_pattern']);
  $seconds = $purifier-&gt;purify($_POST['seconds']);
  
  if(check_answer($answer_pattern, $odpoved)){
  
          $stmt = $db-&gt;prepare("SELECT * FROM odpovedi WHERE uzivatel = :uzivatel AND projekt = :projekt AND otazka = :otazka");
  $stmt-&gt;bindParam(':uzivatel', $user);
  $stmt-&gt;bindParam(':projekt', $projekt);
  $stmt-&gt;bindParam(':otazka', $otazka);
  $stmt-&gt;execute();
  $answers_count = count($stmt-&gt;fetchAll());
  
  if ($answers_count != 1) {
      $stmt = $db-&gt;prepare("INSERT INTO odpovedi (uzivatel, projekt, otazka, odpoved, seconds) VALUES (:uzivatel, :projekt, :otazka, :odpoved, :seconds) ");
      $stmt-&gt;bindParam(':uzivatel', $user);
      $stmt-&gt;bindParam(':projekt', $projekt);
      $stmt-&gt;bindParam(':otazka', $otazka);
      $stmt-&gt;bindParam(':odpoved', $odpoved);
      $stmt-&gt;bindParam(':seconds', $seconds);
  
      $stmt-&gt;execute();
  
      $stmt = $db-&gt;prepare("SELECT * FROM odpovedi WHERE uzivatel = :uzivatel AND projekt = :projekt AND otazka = :otazka");
  $stmt-&gt;bindParam(':uzivatel', $user);
  $stmt-&gt;bindParam(':projekt', $projekt);
  $stmt-&gt;bindParam(':otazka', $otazka);
  $stmt-&gt;execute();
  $answer_inserted = count($stmt-&gt;fetchAll());
  
      if ($answer_inserted &lt; 1) {
          $resp-&gt;success = false;
      } else {
          $resp-&gt;success = true;
      }
  }else{
      $resp-&gt;success = true;
  }
  
  
  
  }else{
      $resp-&gt;success = false;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>print json_encode($resp);</p>
  
  <p>At least alert should be shown, right? But it is not...
  Thank you,</p>
  
  <p>Tomáš</p>