0

我的 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但页面是空白的!?

为什么会发生这种情况?有什么我遗漏的吗?我尝试更改标题位置但没有成功。

谢谢您的帮助!

4

1 回答 1

-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>";
于 2013-05-17T13:16:26.360 回答