我在检索记录时遇到麻烦。我有一个表,它包含记录。我想使用view按钮查看该记录的详细信息。但问题是,当我试图查看记录的所有记录显示相同的细节,即使它们在数据库中是不同的。我在用他们的id检索记录。这是我的代码
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Family Number</th>
<th>Age</th>
<th>Chief of Complaints</th>
<th>Diagnosis</th>
<th>Medication Treatment</th>
<th>Time Added</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php do{ ?>
<tr>
<td class="patient_id"><?php echo $row['id']; ?></td>
<td><?php echo $row['family_number']; ?></td>
<td><?php echo $row['Age'];?></td>
<td><?php echo $row['chief_complaints']; ?></td>
<td><?php echo $row['diagnosis']; ?></td>
<td><?php echo $row['medication_treatment']; ?></td>
<td><?php echo $row['added_at']; ?></td>
<td>
<div class="col-2">
<!-- <a href="view_medicalrecord.php?id=<?php //echo $row['id']; ?>" class="btn btn-primary mb-2">view</a> -->
<a href="#" class="btn btn-primary view_btn mb-2">view</a>
<!-- <div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item view_btn" href="#">View</a></li>
<li><a class="dropdown-item" href="#">Edit</a></li>
</ul>
</div>
</div> -->
</td>
</tr>
<?php }while($row = $medical_itr->fetch_assoc()); ?>
</tbody>
和这是我的js代码检索数据使用他们的id
<script>
$(document).ready(function () {
$('.view_btn').click(function (e) {
e.preventDefault();
var p_id = $(this).closest('tr').find('.patient_id').text();
// console.log(p_id);
$.ajax({
type: "POST",
url: "Modals/viewHistory/view_medical_itr.php",
data: {
'checking_viewbtn': true,
'patient_id': p_id,
},
success: function (response){
// console.log(response);
$('.patient_viewing').html(response);
$('#medical_itr').modal('show');
}
});
});
});
</script>
这是php代码用于检索数据库中的数据
include_once("connections/connections.php");
$con = connection();
if(isset($_POST['checking_viewbtn'])){
$p_id = $_POST['patient_id'];
$sql = "SELECT * FROM medical_itr WHERE id = '$p_id'";
$medical_itr = $con->query($sql) or die ($con->error);
$row = $medical_itr->fetch_assoc();
}
这是样例图片正如你在左下角的图片中看到的,id是31,与url相同,但视图按钮,我试图访问表中的id是21。
我的建议是你在view_button中传递数据id,并在数据id中保持行id。
<a class="btn btn-primary view_btn mb-2" data-id="<?php echo $row['id'];
?>" >view</a>
然后在jquery中获取数据id ->这样的
var p_id = $(this).data('id');