0
我想弄清楚爲什麼有時我的php函數返回一個空數組。觸發ajax調用的事件是當一個下拉改變,然後它碰到我的php函數。大多數情況下,它從數據庫中返回數據,但有時它不會,儘管做了完全相同的事情(改變下拉列表)關於什麼會導致這種情況的任何想法?基本上我使用console.log來檢查返回的內容,有時候什麼也沒有。儘管其他時間使用相同的輸入,但它工作得很好。有時ajax數據庫調用返回空數組?
$(document).ready(function() {
$("#qtrselect").change(function() {
qtrselection = $("#qtrselect").val();
createQtrTable(qtrselection);
$('#qtrselect').prop('disabled', true);
setTimeout(function() {
$('#qtrselect').prop('disabled', false);
}, 1000);
});
});
function createQtrTable(qtrselection, id) {
$.ajax({
type : 'POST',
url : 'createnewtable.php',
data : {qtrdate: qtrselection, id: id},
success : function(response)
{
response = jQuery.parseJSON(response);
totalcommision = 0;
totalpayments = 0;
for (i = 0; i < response.length; i++) {
totalcommision += parseFloat(response[i][0]);
totalpayments += parseFloat(response[i][3]);
}
//I've cut out a bit here as it's not really needed to see the problem.
}
});
}
createnewtable.php
$qtrdate = $_POST['qtrdate'];
$id = $_POST['id'];
$startdate = '';
$enddate = '';
switch ($qtrdate) {
case 0:
$startdate = '2016-01-01' ;
$enddate = '2018-12-30';
break;
case 1:
$startdate = '2016-08-01' ;
$enddate = '2016-10-31';
break;
case 2:
$startdate = '2016-11-01' ;
$enddate = '2017-01-31';
break;
case 3:
$startdate = '2017-02-01' ;
$enddate = '2017-04-30';
break;
case 4:
$startdate = '2017-05-01' ;
$enddate = '2017-07-31';
break;
}
try
{
$stmt = $db_con->prepare("SELECT com.commisionAmount, stu.studentName, pay.paymentDate, pay.paymentAmount FROM `commisions` as com INNER JOIN `accounts` as acc ON com.commisionAccount = acc.id INNER JOIN `studentpayments` as pay ON com.paymentID = pay.id INNER JOIN `students` as stu ON pay.paymentStudent = stu.id WHERE pay.paymentDate BETWEEN '$startdate' AND '$enddate' AND acc.id = '$id'");
$stmt->execute();
$results = $stmt->fetchAll();
$results = json_encode($results);
echo $results;
}
catch(PDOException $e){
echo $e->getMessage();
}
感謝。
謝謝!它不是那麼...它是,但事實並非如此。我意識到我的HTML頁面上有一些較舊的代碼,導致該函數被調用兩次,並且這樣做會導致錯誤! –