自动递增值分配为输入,然后使用值从数据库中删除项目;
Posted
技术标签:
【中文标题】自动递增值分配为输入,然后使用值从数据库中删除项目;【英文标题】:Auto Incrementing value to be assigned as input then using value to delete item from database; 【发布时间】:2021-11-20 23:17:45 【问题描述】:我是一名学习 php 几周的学生。我想做的是生成一个包含所有用户消息的表。然后我需要在每一行中都有一个复选框。当用户点击删除按钮时,带有复选框的行将从表中删除。问题是我无法弄清楚如何为 html 表的每一行分配一个单独的值以使用 PHP 定位。所以我只会删除已经选择的行。我只能删除所有行。
这是我目前得到的结果
<?php
//connect to database
include("../partials/.connect.php");
// select rows from contacts
$query = "SELECT * FROM contacts";
// display table headers
echo '<form>
<table border="0" cellspacing="4" cellpadding="6">
<tr>
<th class="center">ID</th>
<th class="center">Name</th>
<th class="center">Phone</th>
<th class="center">Email</tdclass=center>
<th class="center">Message</th>
<th class="center"> <button type="submit" name="button1">Delete Selected</button</th>
</tr>';
// create loop to fetch all rows from DataBase
if ($result = $conn->query($query))
while ($row = $result->fetch_assoc())
$field1name = $row["id"];
$field2name = $row["name"];
$field3name = $row["phone"];
$field4name = $row["email"];
$field5name = $row["message"];
// If delete button is clicked delete user
if(isset($_POST['button1']))
$sql = "DELETE FROM contacts WHERE id=$field1name";
if ($conn->query($sql) === TRUE)
echo "Record deleted successfully";
else
echo "Error deleting record: " . $conn->error;
// display table data
echo '<tr>
<td class="center">'.$field1name.'</td>
<td class="center">'.$field2name.'</td>
<td class="center">'.$field3name.'</td>
<td class="center">'.$field4name.'</td>
<td class="center">'.$field5name.'</td>
<td class="center"> <input type="checkbox" id="if_checked" name="if_checked"></td>
</tr>
</form>';
$result->free();
?>
</body>
</html>
【问题讨论】:
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或mysqli 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough! 【参考方案1】:你的代码错了,你只是删除了你所有的数据以及你的foreach
运行,所以我只想说,因为你是学生,所以继续学习更多关于编程逻辑的知识
好的,对于您的问题,您必须在之前添加删除查询
$query = "SELECT * FROM contacts";
并且您的删除查询应该基于复选框值的foreach
,最重要的是您的复选框名称应该是一个数组名称,因为您将发布多个具有相同名称的复选框
<input type="checkbox" class="if_checked" name="if_checked[]">
然后你可以使用这样的代码
..................
include("../partials/.connect.php");
if(isset($_POST['button1']))
foreach($_POST['if_checked'] as $id)
......your delete query
// select rows from contacts
$query = "SELECT * FROM contacts";
...................
【讨论】:
【参考方案2】:您可以将复选框用作一个数组,当您选择要删除的复选框时,您将在其中存储 ID,您将遍历复选框并能够删除所有选定的行。
【讨论】:
【参考方案3】:我能够通过将 $row['id'] 变量存储在复选框值而不是 $Field1name 中来解决这个问题。允许我指定要删除的行。
<?php
//connect to database
include("../partials/.connect.php");
// select rows from contacts
$query = "SELECT * FROM contacts";
//if delete button is set
if(isset($_POST['delete']))
$selected = $_POST['if_checked'];
//confirm record target
$confirm = mysqli_query($conn, "SELECT * FROM contacts WHERE id = '$selected'") or die("Not Found");
if(mysqli_num_rows($confirm)>0)
//record found delete
$delete_query = mysqli_query($conn, "DELETE from contacts where id = '$selected'")
or die("Not Deleted");
echo "<div><p>Record Deleted</p></div>";
else
//record not DataBase
die("Not deleted".mysqli_error());
// display table headers
echo '<form method="post" action="" role="form">
<table border="0" cellspacing="4" cellpadding="6">
<tr>
<th class="center">ID</th>
<th class="center">Name</th>
<th class="center">Phone</th>
<th class="center">Email</th>
<th class="center">Message</th>
<th class="center"><input class="center" type="submit" name="delete" value="Delete"></th>
</tr>';
// create loop to fetch all rows from DataBase
if ($result = $conn->query($query))
while ($row = $result->fetch_assoc())
$field1name = $row["id"];
$field2name = $row["name"];
$field3name = $row["phone"];
$field4name = $row["email"];
$field5name = $row["message"];
// If delete button is clicked delete user
// display table data
echo '<tr>
<td class="center">'.$field1name.'</td>
<td class="center">'.$field2name.'</td>
<td class="center">'.$field3name.'</td>
<td class="center">'.$field4name.'</td>
<td class="center">'.$field5name.'</td>
<td class="center">
<input type="checkbox" id="if_checked" name="if_checked" value="'.$row['id'].'"></td>
</tr>
</form>';
$result->free();
?>
</body>
</html>
【讨论】:
以上是关于自动递增值分配为输入,然后使用值从数据库中删除项目;的主要内容,如果未能解决你的问题,请参考以下文章