php 每个表格行的删除按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16191026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete button for each table row
提问by Singed
I manage to succesfully read and display data from my database with the following code: http://pastebin.com/rjZfBWZXI also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
我设法使用以下代码成功读取和显示数据库中的数据:http: //pastebin.com/rjZfBWZX我还为表的每一行生成一个删除按钮:) 单击删除按钮调用“obrisi.php”应该删除该行,但我搞砸了:S 这是 obrisi.php 代码:
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S
我收到解析错误:语法错误,意外的“FROM”(T_STRING):S
回答by Thanasis Pap
Let's try and do it with _GET instead of _POST.
让我们尝试使用 _GET 而不是 _POST 来实现。
In your main code you need to change line 39 (the input) from:
在您的主代码中,您需要将第 39 行(输入)从:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
到:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
在您的 obrisi.php 更改第 3 行(id 设置)中:
$id = $_POST['id'];
to:
到:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
最后,作为一个不错的补充,通过在 obrisi.php 文件末尾添加以下行来重定向回主页面,然后再关闭 php 标记。
header('location:index.php');
Where index.php the name of the main page you have.
其中 index.php 是您拥有的主页的名称。
回答by nvanesch
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
这里有一些简单的错误。这将输出($id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
这条线应该更正为
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
这也出错了。
echo "<form action="obrisi.php" method="post">";
should be like:
应该是这样的:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
但主要问题是帖子中没有给出字段ID。提交时不发送 html 元素的 id。它基本上是在 HTML 结构中识别该元素。
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
当使用提交按钮时,您必须将表单的范围限制为该行并使用隐藏的输入字段,或使用thanpa 建议的链接
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
澄清:如果你想用一个帖子来做(但我建议使用 $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.
并从脚本的开头和结尾删除表单的回声。
回答by MadDokMike
as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.
作为这里的附加说明,如果这将在某个时候在实时系统中使用,您需要检查 obrisi.php 中的 $id 它实际上是一个 ID,而不是像更多 sql 那样令人讨厌和意外的东西,请查找sql注入。