我是 PHP/MySQL 和 HTML 的完全初学者,但是我从这个论坛获得的帮助我学习的帮助是惊人的。真的很感激。
当我从数据库中删除一条记录并将我的 html 表输出到页面上时,我遇到了最近的问题。我点击删除,记录立即被删除,但是如果可能的话,我想要一个对话框说“你确定要删除这个订单吗?”
索引.php:
<?php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform ORDER BY id ASC");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Order Complete?</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td><a href=\"deleterow.php?id=".$row['id']."\">Yes</a></td>";
echo "</tr>";
}
echo "</table>";
?>
删除行.php:
<?php
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
?>
非常感谢所有帮助
最佳答案
像这样尝试,我们删除了 href
并添加了 onclick
,它是在重定向前带有确认提示的 javascript
替换这个:
echo "<td><a href=\"deleterow.php?id=".$row['id']."\">Yes</a></td>";
通过这个:
echo '<td><a href="javascript:void(0);" onclick="if(confirm(\'Are you sure you want to delete this row?\')){ window.location=\'deleterow.php?id='.$row['id'].'\';}">Yes</a></td>';
然后在你的文件 deleterow.php 中保护你自己免受 sql 注入(inject)清理,像这样:
<?php
$id = (int)$_GET['id']; //this needs to be sanitized
if(!empty($id)){
include('connection.php');
$result = mysql_query("DELETE FROM orderform WHERE id=".$id);
}
header("Location: index.php");
?>
如果您开始学习,我相信 SO 上的某个人应该告诉您停止使用 mysql_*
函数,因为它们已被弃用。您应该学习使用:mysqli或 PDO
关于php - 初学者 : PHP Header Error - MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542996/