2013-05-17 18 views
0

我的PHP腳本有一個小問題。我有一個表生成由MySql語句填充的行。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

我沒有看到重定向在你的PHP – michi

+0

你有沒有在該網頁上某種類型的用戶驗證?通過刪除用戶,他們以某種方式搞亂了這個會議? – Pitchinnate

+0

嘗試使用絕對HTTP路徑而不是頭中的相對路徑('location ...')函數。例如,而不是../adminindex.php將其更改爲http://yoursite.com/adminindex.php –

回答

-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>"; 
+0

我寧願遠離病例陳述。對於刪除按鈕,你爲什麼回顯$ row ['username']? – Corey123456

+0

我以爲他想要在其他輸出後刪除按鈕。只是一個例子。但是你是對的,現在我仔細研究了一下,如果(isset)會更好 – Matheno