我想使用PHP和ajax調用函數從我的數據庫中選擇數據。 在select函數中,我使用了textbox
中的$ _POST。我在我的頁面上創建了一個PHP和ajax調用函數,但有些錯誤。我使用isset[]
來檢查我從textbox
獲得的$_POST
數據。我在那裏使用if
條件 - 當$_POST
爲空時,它將選擇所有沒有where子句的數據,並且當特定的$_POST
有值時,它將選擇數據和a where子句。
這裏的問題是,當我使用我的textbox
中的數據將字符串呈現給我的PHP $_POST
時,我的PHP中的else條件無法運行。
這是我的代碼,我使用:
的JavaScript/jQuery的:
$(document).ready(function(e) {
var data = $("#report_all").serialize();
$('#all_report thead').empty();
$('#all_report tbody').empty();
$.ajax({
data: data,
type: "Post",
url: "../php/report/report_all_wjm.php",
success: function(data){
var list = JSON.parse(data);
var th = "";
th += "<th>"+"<center>"+'No'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Storage Location'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Kode Material'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Nama Material'+"</center>"+"</th>";
th += "<th>"+"<center>"+'No.Polisi'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Id Identifier'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Date'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Netto'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Uses'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Unit'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Payroll'+"</center>"+"</th>";
th += "</th>";
$("#all_report thead").append(th);
for(var i = 0; i < list.length; i++){
var tr = "<tr>";
tr += "<td>" +(i+1)+"</td>";
tr += "<td>" +list[i]['sloc']+"</td>";
tr += "<td>" +list[i]['kode']+"</td>";
tr += "<td>" +list[i]['nama']+"</td>";
tr += "<td>" +list[i]['no_pol']+"</td>";
tr += "<td>" +list[i]['id']+"</td>";
tr += "<td>" +list[i]['date']+"</td>";
tr += "<td>" +list[i]['netto']+"</td>";
tr += "<td>" +list[i]['uses']+"</td>";
tr += "<td>" +list[i]['unit']+"</td>";
tr += "<td>" +list[i]['payroll']+"</td>";
tr += "</tr>";
$("#all_report tbody").append(tr);
$("#all_report").show();
}
return false;
}
});
});
PHP:
<?php
include("../../Connections/koneksi.php");
$date_awal=$_POST['date_start'];
$date_akhir=$_POST['date_end'];
$kode=$_POST['kode_mat'];
$kode1=$_POST['kode_mat1'];
$sloc=$_POST['s_loc'];
$sloc1=$_POST['s_loc1'];
$type=$_POST['get_type'];
//Display all data
if (isset($date_awal) == "" || isset($date_akhir) == "" || isset($sloc)== "" || isset($sloc1)== "" || isset($kode)== "" || isset($kode1)== "" || isset($type)== ""){
$sql = "SELECT * FROM wjm ORDER by no asc";
$query = mysqli_query($db,$sql);
$rows = array();
while($tmp= mysqli_fetch_assoc($query)) {
$rows[] = $tmp;
}
}
//Display all data by one date
else {
// Data for Titik1
$sql = "SELECT * FROM wjm WHERE date='$date_awal' order by kode asc ";
$query = mysqli_query($db,$sql);
$rows = array();
while($tmp= mysqli_fetch_assoc($query)) {
$rows[] = $tmp;
}
echo json_encode($rows);
}
mysqli_close($db);
?>
您的代碼易受[** SQL注入**](https://en.wikipedia.org/wiki/SQL_injection)攻擊。您應該通過[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)或[** PDO **](https ://secure.php.net/manual/en/pdo.prepared-statements.php)驅動程序。 [**這篇文章**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)有一些很好的例子。 –
@AlexHowansky嗯..讓我問一些問題。是類似於'$ stmt-> execute'的任何語法,我不知道如何使用' - >'這個語法。我試着用' - >'來使用語法,並且在那裏出現錯誤 –
我發現初學者被掛在PHP對象或PHP OOP上真的很奇怪,但他們很樂意在JavaScript中整天使用它。 –