<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
if ($submit) {
<-- // here if no ID then adding else were editing -->
// 如果没有ID,则我们是在增加记录,否则我们是在修改记录
if ($id) {
$sql = "UPDATE employees SET first=$first,last=$last,
address=$address,position=$position WHERE id=$id";
} else {
$sql = "INSERT INTO employees (first,last,address,position)
VALUES ($first,$last,$address,$position)";
}
<-- // run SQL against the DB -->
// 向数据库发出SQL命令
$result = mysql_query($sql);
<-- echo "Record updated/edited!<p>"; -->
echo "记录修改成功!<p>";
} elseif ($delete) {
<-- // delete a record -->
// 删除一条记录
$sql = "DELETE FROM employees WHERE id=$id";
$result = mysql_query($sql);
<-- echo "$sql Record deleted!<p>"; -->
echo "记录删除成功!<p>";
} else {
<-- // this part happens if we dont press submit -->
// 如果我们还没有按submit按钮,那么执行下面这部分程序
if (!$id) {
<-- // print the list if there is not editing -->
// 如果不是修改状态,则显示员工列表
$result = mysql_query("SELECT * FROM employees",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a f=\"%s?id=%s\">%s %s</a> \n",
$PATH_INFO, $myrow["id"], $myrow["first"], $myrow["last"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><
br>", $PATH_INFO, $myrow["id"]);
}
}
?>
<P>
<a href="<?php echo $PATH_INFO?>">ADD A RECORD</a>
<P>
<form method="post" action="<?php echo $PATH_INFO?>">
<?php
if ($id) {
<-- // editing so select a record -->
// 我们是在编辑修改状态,因些选择一条记录
$sql = "SELECT * FROM employees WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$first = $myrow["first"];
$last = $myrow["last"];
$address = $myrow["address"];
$position = $myrow["position"];
<-- // print the id for editing -->
// 显示id,供用户编辑修改
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<?php
}
?>
<-- First name:<input type="Text"&nb