我有一个表格,用于预览数据库表格中的所有记录。我做了一个按钮,当它被点击时,给定表的所有记录都被删除。我只想在用户点击全部删除后立即添加确认消息。
deleteall.php
// get the 'id' variable from the URL
$id = $_GET['id'];
$table = $_GET['table'];
mysqli_query($con,"DELETE FROM $table");
链接(在另一个php文件中)
echo "<td><a href='../cms/deleteall.php?id=" . "&table=" . $table . "'>Delete All</a></td>";
有什么建议吗?
答案 0 :(得分:7)
AAAHHH!您是否正在使用GET - 不仅要更改内容,还要 - 从服务器中删除内容? Nonono,那是 BAD !
在服务器上进行更改的所有请求应该是POST
个请求。像这样:
<td><form action="../cms/deleteall.php" method="post">
<input type="hidden" name="id" value="<?=$id?>" />
<input type="hidden" name="table" value="<?=$table?>" />
<input type="submit" onClick="return confirm('Are you SURE you want to delete everything?');" value="Delete All" />
</form></td>
此外,确认!我可以非常轻松地将请求更改为delete=users
甚至delete=otherdatabase.tablename
,因为您尚未对其进行验证,并擦除了服务器上的所有数据!要非常小心!
答案 1 :(得分:1)
如果要在单击删除时但在删除发生之前显示确认消息,请使用javascript。
function deleteAll(id,table)
{
var r=confirm("Are you sure you want to delete all?")
if (r==true)
{
location.href = "../cms/deleteall.php?id=" + id + "&table=" + table;
}
}
正如其他人所说,你应该使用$_POST
来代替$_GET
。
答案 2 :(得分:0)
最简单的方法是使用javascript。修改你的链接。
<a href='../cms/deleteall.php?id=" . "&table=" . $table . "' onclick="confirm("Are you sure you want to delete all records? ")">Delete All</a>
另外,我还可以将链接重定向到确认页面。
<a href='../cms/delete_confirm.php?id=" . "&table=" . $table . "' onclick="confirm("Are you sure you want to delete all records? ")">Delete All</a>
delete_confirm.php
<?php ?>
<h1>Would you like to delete all records</h1>
<a href='../cms/deleteall.php?id=".<?php echo $_GET['id'] ?> . "&table=" . <?php echo $_GET['table'] ?> . "'>Delete All</a>
在实践中。你永远不会使用GET方法进行删除或严重的事情。请改用Post。
答案 3 :(得分:0)
您可以将deleteall.php调整为以下内容:
// get the 'id' variable from the URL
$id = $_GET['id'];
$table = $_GET['table'];
if(!isset($_GET['confirm'])){
echo "<a href='../cms/deleteall.php?id=" . $id . "&table=" . $table . "&confirm'>Confirm Delete</a>";
} else {
mysqli_query($con,"DELETE FROM $table");
}
答案 4 :(得分:0)
链接应该是这样的:
echo "<td><a href='../cms/deleteall.php?id=" . "&table=" . $table . "' onclick=\"javascript:return confirm('are you sure you want to do this?');\">Delete All</a></td>";
因此,为了确保在用户确认时只建立一个链接,请使用:
<a href="http://www.amazingjokes.com" onclick="javascript:return confirm('do you want to do this?')">test</a>
答案 5 :(得分:-1)
这可能有所帮助
<script type="text/javascript">
function confirmdeletion() {
var r = confirm("Are you sure you want to delete?");
if (r == true)
{
// code that deletes all
}
else
{
// do nothing or handle cancel
}
}
</script>
你的php如下:
echo "<td><a onClick="confirmdeletion()" href='../cms/deleteall.php?id=" . "&table=" . $table . "'>Delete All</a></td>";