我试图使用bootstrap模式删除确认从我的表中删除一行。 但问题是,当我尝试删除我的表中的一行时,被删除的那一行是第一行(总是)。为什么会这样?有什么想法吗?
HTML (表)
<table class="table table-bordered table-hover clearfix" id="example">
<thead>
<tr>
<th>ID de Funcionário</th>
<th>Sobrenome</th>
<th>Nome</th>
<th>Cargo</th>
<th>Telefone</th>
<th class="text-center">Opção</th>
</tr>
</thead>
<tbody>
<?php
$query="SELECT * FROM employee AS E ORDER BY name";
$res = mysql_query($query);
mysql_set_charset('utf-8');
if(!$res){
echo "Erro ao executar a query";
}
else{
while($dados=mysql_fetch_array($res)){
?>
<tr>
<td><?php echo $dados['func_id']; ?></td>
<td><?php echo $dados['last_name']; ?></td>
<td><?php echo $dados['name']; ?></td>
<td><?php echo $dados['position']; ?></td>
<td><?php echo $dados['phone']; ?></td>
<td class="text-center">
<a class="btn btn-danger btn-xs" data-toggle="modal" data-target="#delete" data-whatever="@mdo">
<span class="fa fa-trash">Apagar</span>
</a>
<?php include('modal_delete.php');?>
</td>
</tr>
<?php
}}
?>
</tbody>
Modal_delete.php (包括)
<div class="modal" id="delete" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-left">Apagar</h4>
</div>
<div class="modal-body text-left">
<p>Deseja apagar este registro?</p>
</div>
<div class="modal-footer">
<a href="../delete/delete_reg.php?id=<?php echo $dados['idemployee'];?>" type="button" class="btn btn-primary">Apagar</a>
<a type="button" class="btn btn-default" data-dismiss="modal">Cancelar</a>
</div>
</div>
</div>
Delete_reg.php
<?php
require("../../conection/conection.php");
$id =$_GET["id"];
$query= "DELETE FROM employee WHERE idemployee= $id";
$delete= mysql_query($query);
if(!$delete){
echo "erro!! não foi possivel apagar dado.";
}
else{
echo"Dado removido com sucesso!";
header("Location: ../list/list_reg.php");
}?>
我在网站上找到的所有可能的解决方案都无法解决我的问题......有人可以帮助我吗?谢谢!!!
答案 0 :(得分:4)
假设您有5行。这意味着对于每一行,您都会生成一个模态。这5个模态中的每一个都有id="delete"
。当您单击链接以打开模态时,引导代码将打开与$('#delete')
匹配的第一个模式,这意味着它将删除第一行。
您有两种解决方案:
您可以在删除按钮的echo $dados['idemployee'];
中添加data-target
,并添加模式的ID。 (不是最佳的,因为您仍然在每个表格行生成模态的代码)
您只制作1个模态,并在删除按钮中添加click()
事件,该按钮会使用正确的ID更新Apagar按钮href
。