我有一个用foreach构造生成的表,在每个<tr>
里面都有一个删除按钮。我想删除包含按钮的行(按ID),但我所做的只是删除保存的最后一行(最后一个ID)而不是所需的。
这是我的HTML代码的一部分:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<table class="Pizarra" id="pizarra" cellspacing="0px";>
<tr class="trThDos">
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<tbody>
<!-- Comienza PHP -->
<?php
$i = 0;
foreach ($resultados as $fila)
{
?>
<tr>
<input type="hidden" name="id" value="<?php echo $fila['ID']; ?>" />
<td class="tdTurno"><?php echo '#' . ++$i ?></td>
<td class="tdImg">
<?php echo $fila ['value'];
switch ($fila['value'])
{
case "0":
echo "<img src='./img/consulta-56-2.png'";
break;
case "1":
echo "<img src='./img/shot-56-2.png'";
break;
case "2":
echo "<img src='./img/ta-56-2.png'";
break;
case "3":
echo "<img src='./img/cert-56-2.png'";
break;
default:
echo "Hubo un error en la selección";
break;
} ?>
</td>
<td>
<?php echo $fila ['nombre']; ?>
</td>
<td class="tdHr">
<?php echo $fila ['hora']; ?>
</td>
<td>
<input type="submit" class="btnBorrar" name="btnBorrar" value="X">
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
PHP代码:
<?php
// ----------- CONEXIÓN ------------------
try {
$conexion = new PDO('mysql:host=localhost;dbname=farmacia', 'Emm', ' ');
$conexion->exec("set names utf8");
//echo "Conexión OK <br />";
}catch(PDOException $e){
echo "Error: " . $e->getMessage();
die();
}
// ----------- TERMINA CONEXIÓN -----------------
if(isset($_POST['btnBorrar'])){
$id = $_POST['id'];
$statement = $conexion->prepare("DELETE FROM eventos WHERE ID = $id");
$statement->execute();
//fetchAll' es clave para que invoque (llame) TODOS los elementos
$resultados = $statement->fetchAll();
header("Location:index.php");
$conexion = null;
}
?>
答案 0 :(得分:0)
不使用POST
方法,而是使用GET
并为删除按钮添加类似
yourscript.php?delete={ID}
并处理您的ID
$deleteId = (int)$_GET["delete"];
和
if(isset($deleteId)){
//do delete stuff
$statement = $conexion->prepare("DELETE FROM eventos WHERE ID = $deleteId");
$statement->execute();
//fetchAll' es clave para que invoque (llame) TODOS los elementos
$resultados = $statement->fetchAll();
}
用此替换隐藏的输入字段;
<a href="yourscript.php?delete=<?php echo $fila['ID']; ?>">Delete me</>
答案 1 :(得分:0)
尝试使用以下代码..
<强> HTML 强>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
<table class="Pizarra" id="pizarra" cellspacing="0px";>
<tr class="trThDos">
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<tbody>
<!-- Comienza PHP -->
<?php
$i = 0;
foreach ($resultados as $fila) {
?>
<tr id="row_id_<?php echo $fila['ID']; ?>">
<td class="tdTurno"><?php echo '#' . ++$i ?></td>
<td class="tdImg"><?php echo $fila ['value'];
switch ($fila['value']){
case "0":
echo "<img src='./img/consulta-56-2.png'";
break;
case "1":
echo "<img src='./img/shot-56-2.png'";
break;
case "2":
echo "<img src='./img/ta-56-2.png'";
break;
case "3":
echo "<img src='./img/cert-56-2.png'";
break;
default:
echo "Hubo un error en la selección";
break;
} ?></td>
<td><?php echo $fila ['nombre']; ?></td>
<td class="tdHr"><?php echo $fila ['hora']; ?></td>
<td><input type="button" class="btnBorrar" name="btnBorrar" value="X" onClick="delete_row(<?php echo $fila['ID']; ?>)"></td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<强>的JavaScript 强>
function delete_row(delete_id) {
$.ajax({
type: "POST",
url: 'delete_row.php',
data: 'delete_id='+delete_id,
success: function(data_respones){
$("#row_id_"+delete_id).remove();
}
});
}
<强> delete_row.php 强>
<?php
// ----------- CONEXIÓN ------------------
try {
$conexion = new PDO('mysql:host=localhost;dbname=farmacia', 'Emm', ' ');
$conexion->exec("set names utf8");
//echo "Conexión OK <br />";
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
die();
}
if(isset($_POST["delete_id"])){
//do delete stuff
$statement = $conexion->prepare("DELETE FROM eventos WHERE ID = $id");
$statement->execute();
}
?>
答案 2 :(得分:0)
使用GET
方法代替POST
在HTML中,而不是使用提交按钮,使用链接,如下所示
<a href="your\php\file\path\delete.php?id=<?php echo $fila['ID']; ?>">X</a>
在delete.php中,请执行以下操作
<?php
if(isset($_GET['id'])){
//get the id into php variable like below
$id = $_GET['id'];
//sql query to delete with id variable in where clause
}
?>
希望这项工作!!!