我想使用 $_GET['student_id'] 显示具有相同 id 的所有行,但数据表错误这是我的代码。例如,一旦我从 url 获取 id : example.com/example.php?=2222
所有 2222 的 id 都会显示出来。
获取.php
<?php
include('db.php');
include('function.php');
$query = '';
$output = array();
$id = $_GET['student_id'];
$query = 'SELECT * FROM personal WHERE student_id LIKE "%'.$_GET["student_id"].'%" ';
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$image = '';
if($row["image"] != '')
{
$image = '<img src="upload/'.$row["image"].'" class="img-thumbnail" width="50" height="35" />';
}
else
{
$image = '';
}
$sub_array = array();
$sub_array[] = $row["student_id"];
$sub_array[] = $row["firstname"]." ".$row['middlename']." ".$row['lastname'];
$sub_array[] = '<a href="edit.php?student_id='.$row["student_id"].'" class="btn btn-info btn-xs update">Personal Info</a>'." ".
'<button type="button" id="'.$row["student_id"].'" class="btn btn-info btn-xs update">Grades</button>'." ".
'<button type="button" id="'.$row["student_id"].'" class="btn btn-info btn-xs update">Payment</button>';
$sub_array[] = '<a href="view.php?student_id='.$row["student_id"].'" class="btn btn-primary btn-xs update">View</a>';
$sub_array[] = '<button type="button" name="delete" id="'.$row["student_id"].'" class="btn btn-danger btn-xs delete">Delete</button>';
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records(),
"data" => $data
);
echo json_encode($output);
?>
数据表调用 我想使用 $_GET['student_id'] 显示具有相同 id 的所有行,但数据表错误这是我的代码。例如,一旦我从 url 获取 id : example.com/example.php?=2222
<script type="text/javascript" language="javascript" >
$(document).ready(function(){
$('#add_button').click(function(){
$('#user_form')[0].reset();
});
var dataTable = $('#user_data').DataTable({
"processing":true,
"serverSide":true,
"order":[],
"ajax":{
url:"fetch.php",
type:"POST"
},
"columnDefs":[
{
"targets":[0, 3, 4],
"orderable":false,
},
],
});
$(document).on('click', '.delete', function(){
var user_id = $(this).attr("id");
if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"delete.php",
method:"POST",
data:{user_id:user_id},
success:function(data)
{
alert(data);
dataTable.ajax.reload();
}
});
}
else
{
return false;
}
});
});
</script>
这是我与过去的问题相关的所有代码。 我想使用 $_GET['student_id'] 显示具有相同 id 的所有行,但数据表错误这是我的代码。例如,一旦我从 url 获取 id : example.com/example.php?=2222
最佳答案
首先,您似乎正在使用 PDO
作为数据库访问层,并且正在准备
您的 SQL
,但是您直接在 SQL 本身内部使用变量犯了一个大错误。如果您已经知道如何准备SQL
,然后使用bind 将变量绑定(bind)到变量名称,请不要这样做,它会打开SQL 注入(inject)
之门。
我还看到您将 $_GET['student_id']
绑定(bind)到 $id
但没有以任何方式使用它,您实际上不需要绑定(bind)收到的变量到新变量。
要回答您的问题,请尝试此操作,
您的代码:
$query = 'SELECT * FROM personal WHERE student_id LIKE "%'.$_GET["student_id"].'%" ';
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
我的建议:
$query = 'SELECT * FROM personal WHERE student_id LIKE :id ';
$statement = $connection->prepare($query);
$statement -> bindParam(':id', $_GET['student_id']);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
关于javascript - 添加 $id = $_GET ['student_id' ];并显示所有相同的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46334380/