title: Ajax: 500 Internal Server Error
body: <p>I am using ajax to get events from my database. Retrieving the results does not work, nothing is displayed, and in the console I get the following error message:</p>
  
  <blockquote>
    <p>POST <a href="http://www.example.com/system/live_filter.php" rel="nofollow">http://www.example.com/system/live_filter.php</a> 500 (Internal Server Error) jquery.min.js:4</p>
  </blockquote>
  
  <p>Here is my <strong>HTML/JS</strong>:</p>
  
  <pre><code>&lt;div id="results"&gt;
  
  &lt;script type="text/javascript"&gt;
  
      // 1. When user comes on page from homepage, results will be fetched with ajax
      function updateResults() { 
  
          // 2. Create array with values of all filter fields
          var value_town_id = $('#town_id').val();
          var value_type = $('#filter_type').val();
          var value_date = $('#filter_date').val();
          var array_filter_values = new Array(value_town_id, value_type, value_date);
          array_filter_values.join(', ');
          query_value = array_filter_values;
  
      // 3. Start ajax
      $.ajax({
              type: "POST",
              url: "system/live_filter.php",
              data: { query: query_value },
              cache: false,
              success: function(html){
                  $("#results").html(html);
              }
          });
  
      };
  
      // 4. FIRE FUNCTION!
      updateResults();
  
  &lt;/script&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>Here is my <strong>live_filter.php</strong> to which the values are sent via Ajax:</p>
  
  <pre><code>require_once 'db.php';
  
  // Define Output HTML Formating
  $html = '';
  $html .= '&lt;div class="event"&gt;';
  $html .= '&lt;h3&gt;titleString&lt;/h3&gt;';
  $html .= '&lt;p&gt;typeString&lt;/p&gt;';
  $html .= '&lt;p&gt;dateString&lt;/p&gt;';
  $html .= '&lt;/div&gt;';
  
  // Get values
  $values_string = $_POST['query'];
  
  // Explode to array
  $values_array = explode(',', $values_string);
  $town_id = $values_array[0];
  $type = $values_array[1];
  $date = $values_array[2];
  
  // Prepare values for database results query
  $town_id = $db-&gt;real_escape_string($town_id);
  $type = $db-&gt;real_escape_string($type);
  $date = $db-&gt;real_escape_string($date);
  
  
  // Build Query
  $query = "SELECT * FROM events WHERE towns_id=$town_id AND type='$type' AND date&gt;=$date";
  
  // Do Search
  $results = $db-&gt;query($query);
  while($result = $results-&gt;fetch_assoc()) {
  
              // Format Output Strings And Hightlight Matches
              $display_title = $result['title'];
              $display_type = $result['type'];
              $display_date = $result['date'];
  
              // Insert title
              $output = str_replace('titleString', $display_title, $html);
              // Insert type
              $output = str_replace('typeString', $display_type, $output);
              // Insert date
              $output = str_replace('dateString', $display_date, $output);
  
              // Output
              echo($output);
  }
  </code></pre>
  
  <p>Anyone has an idea where the problem is?</p>
  
  <p>I get the following error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND type='' AND date>=''' at line 1 </p>