我有一个脚本可以在屏幕上打印表格的所有数据。与每行数据相关联,我有一个删除按钮,我希望,当任何行的按钮被cliked时,该行将被删除。为此,我有以下代码:
$con = mysqli_connect("","","","");
$result = mysqli_query($con,"SELECT * FROM `clientes_pmt`");
while($row = mysqli_fetch_array($result)){
?> <button name="delete" value="<?php echo $row['id']; ?>" type="submit"><img src="paginas/borrar.jpg" /></button>
<a href="page<?php echo $row['nombre']; ?>">
<div><p><?php echo $row['nombre']; ?></p></div>
<div><p><?php echo $row['pais']; ?></p></div>
</a>
<section class="clearboth"></section><br><?php
}
if(isset($_POST['delete'])){
$id = $_POST['delete'];
$result = mysqli_query($con,"DELETE FROM `clientes_pmt` WHERE id = '$id'");
}
mysqli_close($con);
我没有收到任何错误,但该行未被删除。
答案 0 :(得分:3)
将按钮括在form
元素中并设置适当的action
和method
属性,例如:
<form action="/delete.php" method="POST">
<button ...> [your button]
</form>
答案 1 :(得分:0)
你必须修改这些行
$con = mysqli_connect("localhost","root","","YOUR DATABASE NAME");
答案 2 :(得分:0)
要查看结果,您可以将header()
放在此部分代码的末尾:
if(isset($_POST['delete'])){
$id = $_POST['delete'];
$result = mysqli_query($con,"DELETE FROM `clientes_pmt` WHERE id = '$id'");
header('location:your_page_where_the_rows_are.php');
}
答案 3 :(得分:0)
我认为这可能是因为$ id没有被添加到查询中。 试试吼叫?
if(isset($ _ POST [&#39; delete&#39;])){$ id = $ _POST [&#39; delete&#39;]; $ result = mysqli_query($ con,&#34; DELETE FROM clientes_pmt
WHERE id =&#39;&#34;。$ id。&#34;&#39;); }
答案 4 :(得分:0)
使用表单标记和标题在同一页面上重定向您的页面。 试试这个 -
<?php
$con = mysqli_connect("","","","");
$result = mysqli_query($con,"SELECT * FROM `clientes_pmt`");
while($row = mysqli_fetch_array($result)){
?>
<form action="" method="post" >
<button name="delete" value="<?php echo $row['id']; ?>" type="submit"><img src="paginas/borrar.jpg" /></button>
</form>
<a href="page<?php echo $row['nombre']; ?>">
<div><p><?php echo $row['nombre']; ?></p></div>
<div><p><?php echo $row['pais']; ?></p></div>
</a>
<section class="clearboth"></section><br><?php
}
if(isset($_POST['delete'])){
$id = $_POST['delete'];
$result = mysqli_query($con,"DELETE FROM `clientes_pmt` WHERE id = '$id'");
header('location:'.$_SERVER['PHP_SELF']);
}
mysqli_close($con);
?>