gpt4 book ai didi

php - 从数据库检索的网络表中删除行

转载 作者:行者123 更新时间:2023-11-30 00:21:23 25 4
gpt4 key购买 nike

我在从数据库中删除行时遇到问题,这些行已回显到我的网站上,我使用了勾选复选框,当选择多个行时,应删除它们。但这并没有发生!数据库中没有任何内容被删除!请帮忙!

<form method="" action="tester.php">
<?php

include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");

echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>

</tr>";

while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {

for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}


if($result){

}
}
mysql_close();
?>
</form>

最佳答案

首先,您可以implode()从表单中收集的所有 ID 并从那里构建查询。

示例代码:

<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>

<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>

尽管您仍然可以使用 mysqli 或 PDO,但它还是免费的

关于php - 从数据库检索的网络表中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23208847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com