如果你想要一个原始的和工作的解决方案,我建议把一切(逻辑,视图等)放到一个文件中,如下所示。
您可能想要从视图(输出编辑表单,结果等)中分离逻辑(输入检查,表单处理等),显然以下变体在此范围内是“脏的”。
请注意,我没有真正运行这个,所以可能会有一些错过的括号或者什么,并且大部分代码都不存在 - 那里有很多'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