php - 初学者 : PHP Header Error - MySQL

标签 php mysql

我是 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_* 函数,因为它们已被弃用。您应该学习使用:mysqliPDO

关于php - 初学者 : PHP Header Error - MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542996/

相关文章:

mysql - phpMyAdmin - 错误 : relational features are disabled

mysql - 在一组值中搜索一行的其他值

mysql - 减少mysql中的值但不是负数

php - 从 MySQL 分解数据并使用 json_encode 传递值

javascript - 将数组数据从 PHP 传递到 Javascript

php - 如何在mysql中插入\N和带有字符串的制表符?

python - sqlobject线程安全

php自定义错误处理程序如何在生产中打印错误?

php $_POST 选择选项值不起作用未定义索引错误

PHP MySQL InnoDB Unix Windows 区分大小写的表名