初学者:PHP标头错误 - MySQL

时间:2014-03-20 19:17:51

标签: php mysql

我是PHP / MySQL和HTML的完全初学者,但我从这个论坛获得的帮助帮助我学习的帮助令人惊讶。真的很有意思。

当我从数据库中删除记录并将html表输出到页面上时,我的最新问题就出现了。我点击删除并立即删除记录,但是如果可能的话,我会像对话框一样说“你确定要删除此订单吗?”

的index.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>";
?>

deleterow.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");
?>

非常感谢所有帮助

3 个答案:

答案 0 :(得分:3)

尝试这样,我们删除了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注入消毒这样:

<?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

答案 1 :(得分:0)

我就是这样做的...我真的很讨厌javascript弹出窗口......

<?php
if(isset($_GET['id'])){
    $id = $_GET['id'];
    ?>
    <form method="POST">
        <fieldset style="border: thin black solid; width: 500px; margin: 0 auto;">
            <legend>Confirm!</legend>
                Are you sure you want to delete record <b><?=$id?></b><br/><br/>
                <input type="hidden" name="id" value="<?=$id?>"/>
                <input type="submit" name="confirm" value="Yes" />
                <input type="button" value="no" onclick="window.location = 'index.php';" />
        </fieldset>
    </form>
    <?php
}else if(isset($_POST['confirm']) && $_POST['confirm'] == 'YES'){
    $id = $_POST['id'];
    if(isset($id) && !empty($id) && ctype_digit($id)){
        include('connection.php');
        $result = mysql_query("DELETE FROM orderform WHERE id=".$id);
    }
    header("Location: index.php");
    exit();
}

答案 2 :(得分:0)

您可以选择处理此客户端或服务器端。

客户端或浏览器将使用javascript进行处理。对于用户来说,这通常更简单,更容易,但是您的代码对客户端是可见的,可以被操纵或完全绕过。由于这里已经有一个例子,你要求php帮助我将跳过这个例子。

另一个选项是服务器端,或者是php代码。在这里,您将在删除过程中有第二页。此解决方案对于可能会尝试遍历链接的Web爬网程序或机器人可能意外访问的CRUD命令很有用。机器人不会知道该链接是用于删除的,并且会点击它,并且您将在一天早上醒来,每次删除数据库中的记录。

这是一个编写它的方法的简单示例。

<?php
$id = $_GET['id']; //this needs to be sanitized 
$confirm = $_GET['confirm']; //this needs to be sanitized 

if(!empty($comfirm)){
    include('connection.php');
    $result = mysql_query("DELETE FROM orderform WHERE id=$comfirm");
}
?>

<? if(!empty($id)) : ?>
    <h2>Are you sure?</h2>
    <a href="deleterow.php?comfirm=<?= $row['id']?>">Yes</a>
<? endif; ?>

header("Location: index.php");
?>

上面的例子中有两点需要注意。第一,用双引号“和单引号”把东西放在一起。双引号允许您传递变量

echo "Hello $name"; //Hello Jonty

而单引号不是

echo 'Hello $name'; //Hello $name

第二个是你可以在同一个文档中插入html和php,这样你就可以看到我关闭php并开始输入计划HTML,随时我想使用php逻辑我只是使用打开和关闭标签

<? while($row = mysql_fetch_array($result)) : ?>

<tr> 
    <td> <?= $row['id'] ?> </td>
    <td> <?= $row['date'] ?> </td>
    <td> <?= $row['product'] ?> </td>
    <td> <?= $row['productcomments'] ?> </td>
    <td> <?= $row['name'] ?> </td>
    <td> <?= $row['address'] ?> </td>
    <td> <?= $row['age'] ?> </td>
    <td> <?= $row['delivery'] ?> </td>
    <td> <a href="deleterow.php?id= <?= $row['id'] ?>">Yes</a> </td>
</tr>

<? endwhile; ?>

一些帮助你学习php的快速资源是:

http://learnxinyminutes.com/docs/php/

http://www.w3schools.com/PHP/

希望这会有所帮助,快乐的编码。

相关问题