1. allusers.php 里面存放所有数据
`<?php
require_once 'functions.php'
?>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="adduser.html">添加用户</a>
<table>
<tr><th>id</th><th>name</th><th>age</th></tr>
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/30
* Time: 12:04
*/
$conn=connectDb();
mysql_select_db('myapp');
$res=mysql_query("SELECT * FROM users");
$datacount=mysql_num_rows($res);
echo $datacount;
for($i=0;$i<$datacount;$i++){
$resarr=mysql_fetch_assoc($res);
// print_r($resarr);
$id=$resarr['id'];
$name=$resarr['name'];
$age=$resarr['age'];
echo "<tr><td>$id</td><td>$name</td><td>$age</td><td><a href='edituser.php?id=$id'>修改</a></td><td><a href='deletuser.php?id=$id'>删除</a></td></tr>";
}
?>
</table>
</body>
</html>
`
**2. adduser.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="adduser.php" method="get">
姓名:<input type="text" name="name" values="name">
<br><br>
年龄:<input type="text" name="age" values="age">
<input type="submit" values="submit">
</form>
</body>
</html>
adduser.php**
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/30
* Time: 13:43
*/
require_once 'config.php';
if(isset($_GET['name'])){
if(isset($_GET['age'])){
$name=$_GET['name'];
if(empty($name)){
die('user name is empty');
}
$age=$_GET['age'];
if(empty($age)){
die('user age is empty');
}
$conn=connectDb();
mysql_select_db('myapp',$conn);
mysql_query("INSERT INTO users(name,age) VALUES ('$name',$age)");
header('location:allusers.php');
}else{
die ('user age not defined');
}
}else {
die('user name not define') ;
}
?>
3. edituser.php
`<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/30
* Time: 14:22
*/
require_once 'functions.php';
if(isset($_GET['id'])&&!empty($_GET['id'])) {
$id = $_GET['id'];
connectDb();
mysql_select_db('myapp');
$res = mysql_query("SELECT * FROM users WHERE id=$id");
$resarr = mysql_fetch_assoc($res);
$name = $resarr['name'];
$age = $resarr['age'];
print_r($resarr);
}
?>
<form action="edituser_server.php" method="get" >
id:
<input type="text" name="id" value="<?php echo $id; ?>">
姓名:
<input type="text" name="name" value="<?php echo $name; ?>">
<br><br>
年龄:
<input type="text" name="age" value="<?php echo $age; ?>">
<input type="submit" values="submit">
</form>
</body>
</html>
`
edituser_server.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/30
* Time: 15:52
*/
require_once 'functions.php';
if(isset($_GET['name']) && isset($_GET['age']) && isset($_GET['id']) ){
$name=$_GET['name'];
$id=$_GET['id'];
$age=$_GET['age'];
connectDb();
mysql_select_db('myapp');
mysql_query("UPDATE users SET name='$name',age=$age WHERE id=$id");
header('location:allusers.php');
}else
{
die('get wrong');
}
4. deletuser.php
`<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/6/30
* Time: 16:30
*/
require_once 'functions.php';
if(isset($_GET['id'])){
$id=$_GET['id'];
connectDb();
mysql_select_db('myapp');
mysql_query("DELETE FROM users WHERE id=$id");
header ('location:allusers.php');
}else {
die ('please choose one id');
}`
在add和edit是会出现网页输入数据,所以用两个php来实现,一个提供form使用户输入值,另一个用get方法接收数据,并对数据库进行操作。
delete不需要用户输入数据,因此不需要穿件输入的input页面,只增添了一个php页面。