2011-05-22 50 views
0

可能重複:
Adding, deleting and editing users by admin in php如何從數據庫中檢索用戶信息管理在PHP

你好,

我在開發的PHP和我的網站是新我試圖通過創建一個簡單的網站來學習它,主要由兩部分組成: 1.註冊,登錄和網站信息(作爲訪客部分) 2.添加,刪除和編輯用戶信息(作爲管理員部分)

我創建了註冊並登錄,現在我在網站的管理員部分工作。 我將添加的用戶作爲註冊過程,但現在我面臨刪除和編輯用戶的問題。我想要的是在管理頁面中列出數據庫中的所有用戶,以便他可以選擇刪除用戶或編輯他的信息,那麼我該怎麼做?

我試了很多從數據庫檢索用戶,但我失敗了。我知道這個任務可能很簡單,但請原諒,因爲我是這個領域的新手。

僅供參考,我使用的是XAMPP軟件包和記事本。

+2

停止使用記事本,它只會讓事情變得更難。請嘗試使用編程人員的編輯器,如[EditPlus](http://www.editplus.com/)或IDE(如[NetBeans])(http://netbeans.org/)。 – rid 2011-05-22 12:56:02

+2

另一個不錯的選擇是在Windows上的Notepad ++ http://notepad-plus-plus.org/ – 2011-05-22 12:58:38

+0

請更具體一些:) – 2011-05-22 12:59:37

回答

0

如果你想要一個原始的和工作的解決方案,我建議把一切(邏輯,視圖等)放到一個文件中,如下所示。

您可能想要從視圖(輸出編輯表單,結果等)中分離邏輯(輸入檢查,表單處理等),顯然以下變體在此範圍內是「髒的」。

請注意,我沒有真正運行這個,所以可能會有一些錯過的括號或者什麼,並且大部分代碼都不存在 - 那裏有很多'todo'式的註釋。就像一個可能的例子,希望對你有用。

<?php 

// default page view is list users 
$action = isset($_GET['action']) ? $_GET['action'] : 'list'; 

switch ($action) { 

case 'list': 
    // just query for all users and output edit & delete links 
    $query = mysql_query("SELECT id, name /* and anything else needed */ FROM users ORDER BY name"); 
    while($result = mysql_fetch_array($query, MYSQL_ASSOC)) { 
    echo $result['name'] . 
    "(<a href='users.php?action=edit&id={$result['id']}'>edit</a>) " . 
    "(<a href='users.php?action=delete&id={$result['id']}'>delete</a>)<br />"; 
    } 
break; 

case 'edit': 
    // TODO: you probably need to check for the privileges of the user who visits this edit interface. 
    //  compare $id against current user's id, for example! 
    $id = (isset($_GET['id'])) ? intval($_GET['id']) : 0; 
    $query = mysql_query("SELECT * FROM users WHERE id = $id"); 
    if (!mysql_num_rows($query)) { 
    // oops, there's no such user, stop executing the script and output an error 
    } 
    $result = mysql_fetch_array($query, MYSQL_ASSOC); 
    // output your user form here 

    // after the user saves the form, this gets executed 
    if (count($_POST)) { 
    // collect user data, clean it up and save back into database 
    } 

break; 

case 'delete': 
    // checks for ID are the same as in 'edit', make sure the user exists. 
    // TODO: check the current user so that he/she actually could delete users 
    $query = mysql_query("DELETE FROM users WHERE id = {$id}"); // assuming id as good integer. 
    // output some success message 
break; 
} 

?> 
相關問題