上次写好简单的显示页面后,又添加了插入、修改、删除三个基本功能。
index.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta charset="utf-8">
<style>
body {
font: 13px / 1.4 Helvetica, arial;
color: #333;
background-color: #ccc;
}
table {
margin-top: 8px;
margin-bottom: 0px;
color: #666;
background-color: #eee;
}
</style>
<title>书籍一览</title>
</head>
<body>
<form method="post" action="admin.php">
账号:<input type="text" name="user">
密码:<input type="password" name="password">
<input type="submit" name="submit" value="登录">
</form>
<br/>
<table border='2'>
<th colspan='3'>书籍列表</th>
<tr>
<td>书名</td>
<td>价格</td>
<td>描述</td>
</tr>
<?php
#连接数据库
$db = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS);
#选择数据库
mysql_select_db(SAE_MYSQL_DB);
#设置编码方式
mysql_set_charset("utf8");
#查询语句
$query = "select * from Books";
#执行查询,得到结果
$result = mysql_query($query);
#循环输出查询结果
while($rs = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rs["name"]. ""; ?></td>
<td><?php echo $rs["price"]. ""; ?></td>
<td><?php echo $rs["details"]. ""; ?></td>
</tr>
<?php
}
#关闭连接
mysql_close($db);
?>
</table>
</body>
</html>
admin.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta charset="utf-8">
<style>
body {
font: 13px / 1.4 Helvetica, arial;
color: #333;
background-color: #ccc;
}
table {
margin-top: 8px;
margin-bottom: 0px;
color: #666;
background-color: #eee;
}
</style>
<title>管理页面</title>
</head>
<body>
<form method="post" action="index.php">
<input type="submit" name="submit" value="返回">
</form>
<br/>
<?php
$user = $password = "";
$valid = false;
#判断用户名和密码
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user = $_POST["user"];
$password = $_POST["password"];
if ($user == "admin" && $password == "admin") {
$valid = true;
}
}
#登录失败
if ($valid != true) {
echo "Username or password wrong. Please check over.";
die();
}
?>
<?php
#连接数据库
$db = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS);
#选择数据库
mysql_select_db(SAE_MYSQL_DB);
#设置编码方式
mysql_set_charset("utf8");
?>
<form method="post" action="add.php">
书名:<input type="text" name="name" value=""><br/>
价格:<input type="text" name="price" value=""><br/>
描述:<textarea rows="3" cols="50" name="details"></textarea><br/>
<input type="submit" name="submit" value="添加">
</form>
<br/>
<?php
#查询语句
$query = "select * from Books";
#执行查询,得到结果
$result = mysql_query($query);
#循环输出查询结果
while($rs = mysql_fetch_array($result)){
?>
<form method="post" action="update.php">
书名:<input type="text" name="name" value="<?php echo $rs["name"]. ""; ?>"><br/>
价格:<input type="text" name="price" value="<?php echo $rs["price"]. ""; ?>"><br/>
描述:<textarea rows="3" cols="50" name="details"><?php echo $rs["details"]. ""; ?></textarea><br/>
<input type="submit" name="submit" value="修改">
<input type="hidden" name="oldname" value="<?php echo $rs["name"]. ""; ?>">
</form>
<form method="post" action="delete.php">
<input type="submit" name="submit" value="删除">
<input type="hidden" name="name" value="<?php echo $rs["name"]. ""; ?>">
</form>
<br/>
<?php
}
#关闭连接
mysql_close($db);
?>
</table>
</body>
</html>
add.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta charset="utf-8">
<title>添加页面</title>
</head>
<body>
<?php
$result = "No";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$price = $_POST["price"];
$details = $_POST["details"];
#连接数据库
$db = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS);
#选择数据库
mysql_select_db(SAE_MYSQL_DB);
#设置编码方式
mysql_set_charset("utf8");
#插入语句
$query = "insert into Books(name, price, details) values('".$name."','".$price."','".$details."')";
#执行插入
$result = mysql_query($query);
}
echo "". $result. " records updated. ";
?>
<form method="post" action="admin.php">
<input type="submit" name="submit" value="返回">
<input type="hidden" name="user" value="admin">
<input type="hidden" name="password" value="admin">
</form>
</body>
</html>
delete.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta charset="utf-8">
<title>删除页面</title>
</head>
<body>
<?php
$result = "No";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
#连接数据库
$db = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS);
#选择数据库
mysql_select_db(SAE_MYSQL_DB);
#设置编码方式
mysql_set_charset("utf8");
#删除语句
$query = "delete from Books where name = '". $name. "'";
#执行删除
$result = mysql_query($query);
}
echo "". $result. " records updated. ";
?>
<form method="post" action="admin.php">
<input type="submit" name="submit" value="返回">
<input type="hidden" name="user" value="admin">
<input type="hidden" name="password" value="admin">
</form>
</body>
</html>
update.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta charset="utf-8">
<title>更新页面</title>
</head>
<body>
<?php
$result = "No";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$price = $_POST["price"];
$details = $_POST["details"];
$oldname = $_POST["oldname"];
#连接数据库
$db = mysql_connect(SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS);
#选择数据库
mysql_select_db(SAE_MYSQL_DB);
#设置编码方式
mysql_set_charset("utf8");
#更新语句
$query = "update Books set name = '".$name."', price = '".$price."', details = '".$details."' where name = '".$oldname."'";
#执行更新
$result = mysql_query($query);
}
echo "". $result. " records updated. ";
?>
<form method="post" action="admin.php">
<input type="submit" name="submit" value="返回">
<input type="hidden" name="user" value="admin">
<input type="hidden" name="password" value="admin">
</form>
</body>
</html>