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:
POST .php 500 (Internal Server Error) jquery.min.js:4
Here is my HTML/JS:
<div id="results">
<script type="text/javascript">
// 1. When user es 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();
</script>
</div>
Here is my live_filter.php to which the values are sent via Ajax:
require_once 'db.php';
// Define Output HTML Formating
$html = '';
$html .= '<div class="event">';
$html .= '<h3>titleString</h3>';
$html .= '<p>typeString</p>';
$html .= '<p>dateString</p>';
$html .= '</div>';
// 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->real_escape_string($town_id);
$type = $db->real_escape_string($type);
$date = $db->real_escape_string($date);
// Build Query
$query = "SELECT * FROM events WHERE towns_id=$town_id AND type='$type' AND date>=$date";
// Do Search
$results = $db->query($query);
while($result = $results->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);
}
Anyone has an idea where the problem is?
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
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:
POST http://www.example./system/live_filter.php 500 (Internal Server Error) jquery.min.js:4
Here is my HTML/JS:
<div id="results">
<script type="text/javascript">
// 1. When user es 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();
</script>
</div>
Here is my live_filter.php to which the values are sent via Ajax:
require_once 'db.php';
// Define Output HTML Formating
$html = '';
$html .= '<div class="event">';
$html .= '<h3>titleString</h3>';
$html .= '<p>typeString</p>';
$html .= '<p>dateString</p>';
$html .= '</div>';
// 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->real_escape_string($town_id);
$type = $db->real_escape_string($type);
$date = $db->real_escape_string($date);
// Build Query
$query = "SELECT * FROM events WHERE towns_id=$town_id AND type='$type' AND date>=$date";
// Do Search
$results = $db->query($query);
while($result = $results->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);
}
Anyone has an idea where the problem is?
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
Share Improve this question edited May 17, 2014 at 9:55 Kent Miller asked May 17, 2014 at 9:42 Kent MillerKent Miller 5092 gold badges8 silver badges21 bronze badges 2- 4 You need to check the PHP error log on the server for the reason. – Barmar Commented May 17, 2014 at 9:44
-
1
The answer below indicates at least one problem but what you should take from this is how to debug it next time... AJAX Is simply loading a page in the same way your browser does, getting the "source" and putting it in a variable. In this case, you can use Firebug or Chrome developer tools' net tab to see the result of the request. If it were a GET request, you could see the problem easily by just browsing to the URL. Since it's a POST, you can create a fake form to post the appropriate fields to the url or, more simply, change
$_POST['query']
to$_REQUEST['query']
and just browse there. – Basic Commented May 17, 2014 at 9:51
5 Answers
Reset to default 2Can you print the Post value if the variable $town_id is empty then only sql generate error
SELECT * FROM events WHERE towns_id = AND type='$type' AND date>='$date'
in case if you want to execute the query with towns_id is empty. append single with all variables
SELECT * FROM events WHERE towns_id = '$town_id' AND type='$type' AND date>='$date'
Let's tidy this up and simplify it a bit...
function updateResults() {
query = {"town_id": $('#town_id').val(),
"value_type": $('#filter_type').val(),
"value_date": $('#filter_date').val()
}
$.ajax({
type: "POST",
url: "system/live_filter.php",
data: query,
cache: false,
success: function(html){
$("#results").html(html);
}
});
};
and then in the PHP:
require_once 'db.php';
// Define Output HTML Formating
$html = '';
$html .= '<div class="event">';
$html .= '<h3>titleString</h3>';
$html .= '<p>typeString</p>';
$html .= '<p>dateString</p>';
$html .= '</div>';
$town_id = $_REQUEST['town_id'];
$type = $_REQUEST['value_type'];
$date = $_REQUEST['value_date'];
// Prepare values for database results query
$town_id = $db->real_escape_string($town_id);
$type = $db->real_escape_string($type);
$date = $db->real_escape_string($date);
// Build Query
$query = "SELECT * FROM events WHERE towns_id='$town_id' AND type='$type' AND date>='$date'";
/*Should it definitely be towns_id and not town_id?*/
// Do Search
$results = $db->query($query);
while($result = $results->fetch_assoc()) {
// Insert title
$output = str_replace('titleString', $result['title'], $html);
// Insert type
$output = str_replace('typeString', $result['type'], $output);
// Insert date
$output = str_replace('dateString', $result['date'], $output);
// Output
echo($output);
}
Of course if you're willing to move the template, it gets even simpler...
require_once 'db.php';
$town_id = $db->real_escape_string($_REQUEST['town_id']);
$type = $db->real_escape_string($_REQUEST['value_type']);
$date = $db->real_escape_string($_REQUEST['value_date']);
$query = "SELECT * FROM events WHERE towns_id='$town_id' AND type='$type' AND date>='$date'";
$results = $db->query($query);
while($result = $results->fetch_assoc()) {
$html = '<div class="event">';
$html .= '<h3>{$result['title']}</h3>';
$html .= '<p>{$result['type']}</p>';
$html .= '<p>{$result['date']}</p>';
$html .= '</div>';
echo $html;
}
As to why it's erroring...
Firstly, are you sure the variables are being populated? The error you gave in ments would occur if town_id
was missing. Since you're not quoting that field, it would result in broken SQL. It also makes the escaping pointless as the output expects to be in quotes.
I'd also check that the format of the date ing from your form is one that your database understands...
Try changing the PHP to be as follows:
require_once 'db.php';
$town_id = $db->real_escape_string($_REQUEST['town_id']);
$type = $db->real_escape_string($_REQUEST['value_type']);
$date = $db->real_escape_string($_REQUEST['value_date']);
$query = "SELECT * FROM events WHERE towns_id=$town_id AND type='$type' AND date>=$date";
echo $query;
Then take the SQL it gives you and copy/paste it into your database admin tool and see what happens. Once you've fixed the syntax errors there, you'll know how to fix the query in your PHP
There's nothing jumping out at me as syntactically wrong with your PHP. The problem may lie with your MySQL, an include, or anything else. Check your PHP error log for where to look. If you can't find your error log, try
php -l file.php
to run a lint check on all your includes and the file itself in the first instance. Then follow the suggestions above regarding calling it in your browser and looking at any errors.
You need to put the date in quotes in the query:
$query = "SELECT *
FROM events
WHERE towns_id=$town_id AND type='$type' AND date>='$date'";
You should also add error checking to your code:
$results = $db->query($query) or die($db->error);
Another problem: In your Javascript, you're joining the query values with ', '
(with a space after the ma), but in PHP you're exploding with just ','
(no space). You should be consistent. Even better would be to send all the query values as separate parameters:
data: { id: value_town_id, type: value_type, date: value_date }
Then you can access them in PHP with $_POST['id']
, $_POST['type']
, and $_POST['date']
.
The problem is with your javascript.
array_filter_values.join(', ');
query_value = array_filter_values;
This is wrong, try this:
query_value =array_filter_values.join(', ');
array_filter_values.join(', ') returns the array joined to a string, it doesn't convent the array to a joined string.