1.查询:
数据的显示,这里就可以嵌入php来进行数据的输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "content-type" content= "text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td>代号</td>
<td>名称</td>
<td>性别</td>
<td>生日</td>
<td>班级</td>
<td>操作</td>
</tr>
<?php
$db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" );
//连接数据库
$sql = "select * from student " ;
//写sql语句
$r = $db ->query( $sql );
//执行sql语句返回给r
if ( $r ) //条件
{
while ( $attr = $r ->fetch_row())
{
$ssex = "" ;
if ( $attr [2])
{
$ssex = "男" ;
}
else
{
$ssex = "女" ;
}
echo
"
<tr>
<td>{ $attr [0]}</td>
<td>{ $attr [1]}</td>
<td>{ $ssex }</td>
<td>{ $attr [3]}</td>
<td>{ $attr [4]}</td>
//添加一个单击事件,防止不小心删掉
<td><a onclick=\ "return confirm('确定要删除吗???')"
href= 'shanchu.php?sno={$attr[0]}' >删除</a>
<a href= 'xiugai.php?sno={$attr[0]}' >修改</a> </td> </tr>"; } } ?>
</table>
<a href= "tianjia.php" rel= "external nofollow" >添加页面</a>
</body>
</html>
|
2.删除的处理页面
删除时是链接到删除处理页面的,所以还要写一个删除处理页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
$aaa = $_get [ "sno" ]; //删除方式使用的get,照旧
$db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" );
//连接...
$sql = "delete from student where sno='{$aaa}'" ;
//写sql语句,sno主键
if ( $db ->query( $sql )) //执行sql语句
{
header( "location:text.php" );
//删完回去表页面
}
else {
echo "删除失败" ;
}
?>
|
来张效果图:
3.添加数据:
点击即可进入添加页面
添加页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<body>
<h1>添加</h1>
<form action= "add.php" method= "post" >
<div>代号:<input type= "text" name= "sno" /></div>
<div>名字:<input type= "text" name= "sname" /></div>
<div>性别: <input type= "radio" value= "1" name= "sex" />男
<input type= "radio" value= "0" name= "sex" />女</div>
<div>日期:<input type= "text" name= "sbirthday" /></div>
//创建表时性别是用的1或2来表示的,要是进行修改不知道1或2代表了什么,所以就要进行处理,处理成用户能够明白的男和女
<div>班级:
<select name= "class" >
<?php
$db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" );
//连接...
$sql = " select * from class " ;
//写sql...
$r = $db ->query( $sql );
//执行...返回...
while ( $arr = $r ->fetch_row())
{
echo "<option value='{$arr[0]}'>{$arr[1]}</option>" ;
//添上以后回表页面
}
?>
</select>
</div>
<div><input type= "submit" value= "添加" /></div>
</form>
</body>
|
添加也需要一个处理页面来判断添加:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
$sno = $_post [ "sno" ];
//$_post 变量用于收集来自 method="post" 的表单中的值。
$sname = $_post [ "sname" ];
$ssex = $_post [ "ssex" ];
$sbirthday = $_post [ "sbirthday" ];
$class = $_post [ "class" ];
$db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" );
$sql = "insert into student values ('{$sno}','{$sname}','{$ssex}','{$sbirthday}','{$class}')" ;
//向数据库中添加写的数据
if ( $db ->query( $sql ))
{
header( "location:text.php" );
//header() 函数向客户端发送原始的 http 报头。
}
else {
echo "添加失败" ;
}
?>
|
效果图:
4.修改数据:主键不可修改!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "content-type" content= "text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<h1>修改</h1>
<?php
$sno = $_get { "sno" };
$db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" );
$sql = "select * from student where sno='{$sno}'" ;
$r = $db ->query( $sql );
$arr = $r ->fetch_row();
?>
<form action= "update.php" method= "post" >
<div>代号:<input readonly= "readonly" type= "text" name= "sno" value="<?php
echo $arr [0];
?>"/></div>
//readonly只可读
<div>名称:<input type= "text" name= "sname" value= "<?php echo $arr[1]; ?>" /></div> <div>性别: <input type= "radio" name= "ssex" value= "1" <?php echo $arr [2]? "checked='checked'" : "" ; ?>/>男 <!-- 三元运算符,如果性别=ture,默认值就在男上面,否则空--> <input type= "radio" name= "ssex" value= "0" <?php echo $arr [2]? "" : "checked='checked'" ; ?>/>女 </div> <div>日期:<input type= "text" name= "sbirthday" value= "<?php echo $arr[3]; ?>" /></div> <div>班级:<select name= "class" >
//value取默认值
<?php
$sclass = "select * from class" ;
$rclass = $db ->query( $sclass );
while ( $attr = $rclass ->fetch_row())
//取到的班级信息
{
//判断将要输出的班级是不是和该人员的是否相同
if ( $arr [4]== $attr [0]) //arr是班级名,attr是班级的代号,俩表
{
echo "<option value = '{$attr[0]}' selected='selected'>{$attr[1]}</option>" ;
}
else {
echo "<option value = '{$attr[0]}'>{$attr[1]}</option>" ;
}
}
?>
</select></div>
<div><input type= "submit" value= "修改完毕" /></div>
</form>
</body>
</html>
|
]
修改的处理页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
$sno = $_post [ "sno" ];
$sname = $_post [ "sname" ];
$ssex = $_post [ "ssex" ];
$sbirthday = $_post [ "sbirthday" ];
$class = $_post [ "class" ];
$db = new mysqli( "localhost" , "root" , "12345678" , "heiheihei" );
$sql = "update student set sname= '{$sname}' ,
ssex= '{$ssex}' ,
sbirthday= '{$sbirthday}' ,
class = '{$class}' where sno= '{$sno}' ";
//看一下是不是传递过来的sno值;
if ( $db ->query( $sql ))
{
header( "location:text.php" );
}
else {
echo "修改失败" ;
}
?>
|
修改的效果图: