很好的一天!我得到一个错误,上面写着“未定义的变量:id”。
我创建了一个表,每一行都有一个delete按钮,单击它会弹出一个模式,询问是否要删除该行。但是当我点击delete按钮时,它会显示error undefined variable:id,并且不会删除数据。
这是我的表格代码:
if(isset($_POST['delete'])) {
$data->delete_multiple($id, $conn);
}
<table class="table table-hover table-bordered" id="table1">
<thead>
<tr>
<th>Faculty Code</th>
<th>Last Name</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Position</th>
<th></th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result -> fetch_object()): ?>
<tr>
<td><?php echo $row->Faculty_ID;?></td>
<td><?php echo $row->Faculty_Lastname ?></td>
<td><?php echo $row->Faculty_Firstname ?></td>
<td><?php echo $row->Faculty_Middlename?></td>
<td><?php echo $row->Position; ?></td>
<td class="text-center">
<a class="btn btn-sm btn-outline-primary text-muted" href="Faculty_edit.php?Faculty_ID=<?php echo $row->Faculty_ID;?>">Edit</a>
<a type="button" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal" class="btn btn-sm btn-outline-danger" >Delete</button></td>
<td>
<?php if($row->Status=='Active') echo '<a href="#deactive_account" data-toggle="modal" data-id='.$row->Faculty_ID.' class="activate" style="color:green;">Active</a>'; ?>
<?php if($row->Status=='Inactive') echo '<a href="#active_modal" data-toggle="modal" data-id='.$row->Faculty_ID.' class="activate" style="color:red;">Inactive</a>'; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
我的模式代码:
<div class="modal fade" id="delete_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Confirmaiton</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="alert alert-danger" role="alert">
Are you sure you want to delete selected record?
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-danger" name="delete">Yes</button>
</div>
</div>
</div>
</div>
删除功能代码:
function delete_multiple($id, $conn){
$id = $_POST['delete'];
$sql = "DELETE FROM faculty WHERE Faculty_ID=? ";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
}
1条答案
按热度按时间8fsztsew1#
这是因为您没有将行id传递给模式弹出窗口。
将教员id添加到“删除”按钮,并向其添加唯一类“删除教员btn”。参考下线
将模式弹出式页脚更改为:-
并添加以下javascriptscript:-
我的假设是,在html表单的模式弹出窗口中有submit按钮。