php 从表中显示的数据库中插入特定行的删除按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18988680/
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
insert delete button of specific row from a database displayed in table
提问by Vladimir Vasilev
Hello I have the following code displaying a database results, in the 3th TD of the table I would like to insert delete button which will delete the table record of the data which is next to the button, what should be the code in the 3th TD of the tabe for the delete button ?
您好,我有以下代码显示数据库结果,在表的第 3 个 TD 我想插入删除按钮,该按钮将删除按钮旁边的数据的表记录,第 3 个 TD 中的代码应该是什么删除按钮的标签?
<?php
$con=mysqli_connect("localhost","table","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM recetas_galletas");
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Description</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . DELETE BUTTON "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
采纳答案by Prix
There are several ways to archive this, first and and most important is that you need a field on your database table where you can identify the record you want to delete, for example a primary key in form of an ID or a unique key.
有几种方法可以存档,首先,也是最重要的是,您需要在数据库表上有一个字段,您可以在其中标识要删除的记录,例如 ID 形式的主键或唯一键。
You can do this by creating a link with a text to a delete.php
page or you can use JQuery and AJAX or you can use a inner form.
您可以通过创建带有指向delete.php
页面的文本的链接来实现此目的,或者您可以使用 JQuery 和 AJAX,或者您可以使用内部表单。
You will also want to have only authorized users to use those pages so you will need a login page with session as well.
您还希望只有授权用户才能使用这些页面,因此您还需要一个带有会话的登录页面。
You can see here an example of login page with sessions.
The simplest one is a link to the delete page, see example here:
最简单的是删除页面的链接,请参见此处的示例:
<?php
$con = mysqli_connect("localhost","table","password","database");
// Check connection
if (mysqli_connect_errno())
{
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
if (!$result = mysqli_query($con,"SELECT * FROM recetas_galletas"))
{
die("Error: " . mysqli_error($con));
}
?>
<table border='1'>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><a href="delete.php?id=<?php echo $row['id']; ?>">Delete</a></td>
</tr>
<?php
}
mysqli_close($con);
?>
</table>
Then on your delete page you would have something like this:
然后在您的删除页面上,您会看到如下内容:
<?php
// Your database info
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
if (!isset($_GET['id']))
{
echo 'No ID was given...';
exit;
}
$con = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
{
die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}
$sql = "DELETE FROM recetas_galletas WHERE id = ?";
if (!$result = $con->prepare($sql))
{
die('Query failed: (' . $con->errno . ') ' . $con->error);
}
if (!$result->bind_param('i', $_GET['id']))
{
die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}
if (!$result->execute())
{
die('Execute failed: (' . $result->errno . ') ' . $result->error);
}
if ($result->affected_rows > 0)
{
echo "The ID was deleted with success.";
}
else
{
echo "Couldn't delete the ID.";
}
$result->close();
$con->close();
回答by Ahmed Essam
You can make a link that passes the id to another php page which preforms the delete. So the link will be like :
您可以创建一个链接,将 id 传递到另一个执行删除操作的 php 页面。所以链接将是这样的:
echo "<td><a href='http://www.site.com/delete.php?id=" . $row['id'] . "'></td>";
then in your delete.php :
然后在您的 delete.php 中:
if (is_int($_GET["id"]) {
$query = "DELETE FROM recetas_galletas WHERE id = " . $_GET["id"];
$result = mysqli_query($con, $query);
// Check the result and post confirm message
}
This is a very simple way of doing it but it might not be the best for you .If you didn't verify the user of this page, any one could manipulate this code easily and delete any row of the table.
这是一种非常简单的方法,但它可能不是最适合您的方法。如果您没有验证此页面的用户,任何人都可以轻松操作此代码并删除表中的任何行。
Other ways could be navigating to the same page and do the same process in the same page or you could use ajax but this is another issue.
其他方法可能是导航到同一页面并在同一页面中执行相同的过程,或者您可以使用 ajax,但这是另一个问题。
Hope this helps and excuse my English.
希望这会有所帮助并原谅我的英语。
回答by Bjjs JKD
echo "
<form action='user.php' method='post'>
<table cellpadding='2' cellspacing='2' border='2' >
<tr>
<td>Id</td>
<td>Name</td>
<td>Gender</td>
<td>Action</td>
</tr>
";
$select = "select * from user";
$result = mysqli_query($con,$select);
while($r = mysqli_fetch_row($result))
{
echo "
<tr>
<td>$r[0]</td>
<td>$r[1]</td>
<td>$r[2]</td>
<td><input type='submit' value='Delete $r[0]' style='width:53px;' name='submit' ></td>
</tr>
";
}
echo "
</table>
</form>
";
if ($_POST['submit'])
{
$id = $_POST['submit'];
$id = end(explode(" ",$id));
$delete = "delete from user where id=$id";
mysqli_query($con,$delete);
header("Location:user.php");
}
?>