美好的一天, 我是PHP/MySQL的新手,嘗試從ajax發送請求,並在具有多個參數的數據庫中選擇一行。 這是做這件事的好方法嗎?選擇具有多個參數的行(ajax和PHP)
AJAX(jQuery的):
function readLine(name, firstname) {
$.ajax({
type: "post",
url: "./php/readLine.php",
dataType: 'json',
data: { name: name, firstname: firstname },
success: function(data) {
console.log(data);
}
error: function(data) {
console.log("An error occured!");
}
});
}
PHP:
<?php
$sql = "SELECT * FROM table1 WHERE firstname=".intval($_POST['firstname'])." AND name=".intval($_POST['name']);
$con = mysqli_connect("localhost", "root", "", "myDB");
if (!$con) {
die("Connection failed: " . mysqli_error($con));
}
$result = mysqli_query($con, $sql);
$to_encode = array();
while($row = mysqli_fetch_array($result, MYSQLI_NUM)){
$to_encode[] = $row;
}
echo json_encode($to_encode);
mysqli_close($con);
?>
感謝您的幫助。
對不起,但這是最糟糕的做法。您對***注入攻擊持開放態度。研究'準備好聲明'以獲得更好的方法 –
'name'和'firstname'是字符串...所以我想我不應該在我的PHP代碼中使用'intval'...但是我應該使用什麼呢? – EricF