新浪云PHP(续)

上次写好简单的显示页面后,又添加了插入、修改、删除三个基本功能。

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值