PHP MySQL删除重定向到空白页面

时间:2013-05-17 13:09:10

标签: php mysql form-submit delete-row

我的php脚本有点问题。我有一个表生成由MySql语句填充的行。

在最后一栏,我有一个编辑按钮,然后删除。我的问题是,当我点击删除时,查询成功运行,但它将我重定向到空白页面!

标题位置是正确的但是当我点击删除时它会保留在当前页面上,但它只是一个纯白页。

<?php foreach($rows as $row): ?> 
<tr> 
    <td>
        <form action="" method="post"> <?php echo $row['id']; ?> </form>
    </td>
    <td>
        <form action="" method="post"> <?php echo $row['roleid']; ?> </form>
    </td>
    <td>
        <form action="" method="post">
            <?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8'); ?>
        </form>
    </td>
    <td>
        <form action="" method="post">
            <?php echo htmlentities($row['email'], ENT_QUOTES, 'UTF-8'); ?>
        </form>
    </td>
    <td>
        <form action="" method="post">
            <input name="Edit" type="submit" value="Edit" />
            <input name="id" type="hidden" value="<?php echo $row['id']; ?>" />
        </form>
    </td>
</tr> 
<?php endforeach; ?>

我可以使用以下方式成功设置会话:

if (isset($_POST['Edit'])) {

$_SESSION['id'] = $_POST['id'];
header("Location: edit_account.php");

}

但似乎我遇到了另一个问题:(我还想在每一行添加一个删除按钮来删除该用户帐户。现在这就是它的样子:

<td> <form action="" method="post">
        <input name="Delete" type="submit" value="Delete" />
        <input name="id" type="hidden" value="<?php echo $row['id']; ?>" />
    </form> </td>

使用的php代码是:

if (isset($_POST['Delete'])) {
    // Everything below this point in the file is secured by the login system 

    // We can retrieve a list of members from the database using a SELECT query. 
    // In this case we do not have a WHERE clause because we want to select all 
    // of the rows from the database table. 
    $query = " 
        DELETE 
        FROM user
        WHERE
            id = :id 
    "; 

    // The parameter values 
    $query_params = array( ':id' => $_POST['id'] ); 

    try { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    }

    catch(PDOException $ex) { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt->fetch(); 

    // This redirects the user back to the members-only page after they register 
    header("Location: ../adminindex.php"); 

    // Calling die or exit after performing a redirect using the header function 
    // is critical.  The rest of your PHP script will continue to execute and 
    // will be sent to the user if you do not die or exit. 
    die("Redirecting to adminindex.php.php"); 
}

我的问题是重定向!当我点击“删除”按钮时,它实际上会运行查询,但之后它只是重定向到 memberlist.php ,但页面是空白的!?

为什么会这样?有没有我缺少的东西?我尝试更改标题位置但没有成功。

感谢您的帮助!

1 个答案:

答案 0 :(得分:-1)

die("Redirecting to adminindex.php.php"); ??

为什么不使用开关? 像这样:

switch($action){
    case 'delete':
        //your code here
    break;
    case 'edit':
        //your code here
    break;
}

并执行删除按钮:

echo $row['username'] ."<a href=page.php?action=delete&id=".$row['id']."><img src=some fancy img></a>";