我已经将我的database table
回显到我的网站上,并在表格旁边有勾选选项,您可以在其中tick
它并delete
网站中的任何条目,它应该从数据库中删除它。由于某种原因它不起作用,我是初学者,任何帮助将不胜感激。
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$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 = $checkbox[$i];
print_r($_GET['delete_these']);
$ids = implode(', ', $_POST['delete_these']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
}
}
mysql_close();
?>
</form>
你在表单标签中添加了方法--
<form method="post" action="tester.php" >
并将if(isset($_GET['delete']))
替换为if(isset($_POST['delete']))
将表单标记替换为:
<form method="post" action="tester.php">
那么这3个:
if(isset($_POST['delete']))
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE id=$id";
在这 3 个地方:
if(isset($_GET['delete']))
$id = $checkbox[$i];
$sql = "DELETE FROM booking WHERE id IN($ids)";
您的$id = $checkbox[$i];
仅在register_globals
指令打开时才有效,由于它引起的安全问题,该指令现已弃用(并从 PHP 5.4 中删除(,如果它存在于您使用的大多数托管服务的 PHP 版本中,请将其关闭。
您将通过 post 发送数据,以便通过 $_POST
访问它们。 $_POST
之前(int)
是将任何内容转换为整数,因此如果用户尝试发送字符串,它将变得0
,这将保护您免受SQL注入。
旁注 您应该停止使用 mysql_* 函数,因为它们已被弃用。查看PDO或mysqli