我正在使用 ajax 从我的数据库中获取事件。检索结果不起作用,没有任何显示,并且在控制台中我收到以下错误消息:
POST http://www.example.com/system/live_filter.php 500 (Internal Server Error) jquery.min.js:4
这是我的HTML/JS:
<div id="results">
<script type="text/javascript">
// 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();
</script>
</div>
这是我的 live_filter.php,值通过 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);
}
有人知道问题出在哪里吗?
我收到以下错误消息:您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“AND type='' AND date>=''' 附近使用的正确语法
最佳答案
如果变量$town_id 为空 那么只有sql 产生错误,你能打印Post 值吗
SELECT * FROM events WHERE towns_id = AND type='$type' AND date>='$date'
如果你想执行 towns_id 为空的查询。将所有变量附加到 single
SELECT * FROM events WHERE towns_id = '$town_id' AND type='$type' AND date>='$date'
关于javascript - Ajax:500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23709830/