简体   繁体   English

删除每个表行的按钮

[英]Delete button for each table row

I manage to succesfully read and display data from my database with the following code: http://pastebin.com/rjZfBWZX I 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代码:

http://pastebin.com/mrFy1i7S http://pastebin.com/mrFy1i7S

I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S 我收到一个Parse错误:语法错误,意外'FROM'(T_STRING):S

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. 最后作为一个很好的补充,通过在关闭php标记之前在obrisi.php文件的末尾添加以下行来重定向回主页面。

    header('location:index.php');

Where index.php the name of the main page you have. 其中index.php是您拥有的主页面的名称。

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. 但主要问题是帖子中没有给出字段ID。 The id of a html element is not sent on submit. 提交时不会发送html元素的id。 it is basically to identify that element in the HTML structure. 它基本上是在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. 并从脚本的开头和结尾删除表单的echo。

作为一个额外的注释,如果这将在某个时候在实时系统中使用,你需要在obrisi.php中检查$ id,它实际上是一个ID,而不是像sql一样令人讨厌和意外,查找sql注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM