如果你想要一個原始的和工作的解決方案,我建議把一切(邏輯,視圖等)放到一個文件中,如下所示。
您可能想要從視圖(輸出編輯表單,結果等)中分離邏輯(輸入檢查,表單處理等),顯然以下變體在此範圍內是「髒的」。
請注意,我沒有真正運行這個,所以可能會有一些錯過的括號或者什麼,並且大部分代碼都不存在 - 那裏有很多'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;
}
?>
停止使用記事本,它只會讓事情變得更難。請嘗試使用編程人員的編輯器,如[EditPlus](http://www.editplus.com/)或IDE(如[NetBeans])(http://netbeans.org/)。 – rid 2011-05-22 12:56:02
另一個不錯的選擇是在Windows上的Notepad ++ http://notepad-plus-plus.org/ – 2011-05-22 12:58:38
請更具體一些:) – 2011-05-22 12:59:37