项目最终的效果
实现步骤:
创建目录
做前端
把公共部分放到对应的目录
做后端
1.创建目录
说明:admin目录存放后端的文件,config存放配置文件,inc存放公共文件,style存放css层叠样式表文件。
2.前端部分
<!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> <link rel="stylesheet" type='text/css' href='style/common.css' /> </head> <body> <?php require $_SERVER['DOCUMENT_ROOT'].'/project/inc/head.php'?> <?php require $_SERVER['DOCUMENT_ROOT'].'/project/inc/conn.php'; $titleid=isset($_GET['titleid'])?$_GET['titleid']:1; $sql="select * from student where c_id=$titleid order by id desc"; $rs=mysql_query($sql); ?> <table> <tr> <th>序号</th> <th>姓名</th> <th>性别</th> <th>学号</th> </tr> <?php while($row = mysql_fetch_assoc($rs)): ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['sex']; ?></td> <td><?php echo $row['num']; ?></td> </tr> <?php endwhile?> </table> </body> </html>
3.公共文件&配置文件
3-1. 公共head
<?php //导入连接数据库文件 ?> <?php require $_SERVER['DOCUMENT_ROOT'].'/project/inc/conn.php' ?> <?php //查询数据库文件 $sql="select * from class"; //执行mysql代码 $rs=mysql_query($sql); ?> <table> <tr> <?php //循环取出数据库的数据 while($row = mysql_fetch_assoc($rs)): ?> <th><a href='?titleid=<?php echo $row['id']?>'><?php echo $row['classname']; ?></a></th> <?php endwhile ?> </tr> </table>
3-2.公共css
@charset "utf-8"; /* CSS Document */ *{ margin:0; padding:0; } table{ width:800px; margin:50px auto; } th,td{ border:1px solid #808080; }
3-3.配置文件
<?php return array( //主机名 'host' => 'localhost', //端口号 'port' => '3306', //用户名 'user' => 'root', //密码 'pwd' => '123456', //数据库 'database' => 'my_database', //字符集 'charset' => 'utf8' );
4.后端部分
4-1. 后端首页
<!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> <link rel="stylesheet" type='text/css' href='../style/common.css' /> </head> <body> <?php require $_SERVER['DOCUMENT_ROOT'].'/project/inc/head.php'?> <?php require $_SERVER['DOCUMENT_ROOT'].'/project/inc/conn.php'; $titleid=isset($_GET['titleid'])?$_GET['titleid']:1; $sql="select * from student where c_id=$titleid order by id desc"; $rs=mysql_query($sql); ?> <table> <tr> <th>序号</th> <th>姓名</th> <th>性别</th> <th>学号</th> <th>操作</th> </tr> <?php while($row = mysql_fetch_assoc($rs)): ?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['sex']; ?></td> <td><?php echo $row['num']; ?></td> <td><a href="/project/admin/add.php?titleid=<?php echo $titleid?>">增加</a>|<a href="/project/admin/del.php?id=<?php echo $row['id']?>&titleid=<?php echo $titleid?>">删除</a>|<a href="/project/admin/update.php?titleid=<?php echo $titleid?>&name=<?php echo $row['name']?>&num=<?php echo $row['num']?>&sex=<?php echo $row['sex']?>&id=<?php echo $row['id']?>">修改</a></td> </tr> <?php endwhile?> </table> </body> </html>
4-2.后端功能实现
增
<!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> <link rel="stylesheet" type="text/css" href="../style/common.css"> <style type='text/css'> table{ width:300px; } th{ width:80px; } </style> <script tpye='text/javascript'> function gaga(){ var name=document.getElementById('name').value; var num=document.getElementById('num').value; if(name==''||num==''){ alert('你的姓名或学号为空'); document.getElementById('name').focus(); return false; } } </script> </head> <body> <?php require $_SERVER['DOCUMENT_ROOT'].'/project/inc/conn.php'?> <?php $titleid=$_GET['titleid']?> <?php if(isset($_POST['submit'])){ $name=$_POST['name']; $num=$_POST['num']; $classname=$_POST['classname']; $sex=$_POST['sex']; if(trim($name)==''||trim($num)==''){ echo "你咋这么坏呢"; exit; } $sql="insert into student values(null,'$name','$num',$classname,'$sex')"; $rs=mysql_query($sql); if(!$rs) echo '学号重复!'; else echo '添加成功!'; header('location:admin.php?titleid='.$classname); } ?> <?php $sql='select * from class'; $rs=mysql_query($sql); ?> <form method='post' action='' onsubmit="return gaga()"> <table> <tr> <th>姓名:</th> <td><input type='text' name='name' id='name'></td> </tr> <tr> <th>学号:</th> <td><input type='text' name='num' id='num'></td> </tr> <tr> <th>班级:</th> <td><select name='classname' id='classname'> <?php while($cs=mysql_fetch_assoc($rs)):?> <option value='<?php echo $cs['id']?>' <?php if($cs['id']==$titleid) echo 'selected'?>><?php echo $cs['classname']?></option> <?php endwhile?> </select></td> </tr> <tr> <th>性别:</th> <td><input type='radio' name='sex' value='男' checked>男 | <input type='radio' name='sex' value='女'>女 </td> </tr> <tr> <th>操作:</th> <td><input type='submit' name='submit' value='添加'>|<input type='reset' name='submit' value='重置'></td> </tr> </table> </form> </body> </html>
删
<?php require $_SERVER['DOCUMENT_ROOT'].'/project/inc/conn.php'; $id=$_GET['id']; $titleid=$_GET['titleid']; $sql="delete from student where id=$id"; $rs=mysql_query($sql); if(!$re) echo '没有这条记录'; else echo '删除成功'; header('location:admin.php?titleid='.$titleid);
改
<!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> <link rel="stylesheet" type="text/css" href="../style/common.css"> <style type='text/css'> table{ width:300px; } th{ width:80px; } </style> <script tpye='text/javascript'> function gaga(){ var name=document.getElementById('name').value; var num=document.getElementById('num').value; if(name==''||num==''){ alert('你的姓名或学号为空'); document.getElementById('name').focus(); return false; } } </script> </head> <body> <?php require $_SERVER['DOCUMENT_ROOT'].'/project/inc/conn.php'?> <?php $titleid=$_GET['titleid']; $name=$_GET['name']; $num=$_GET['num']; $sex=$_GET['sex']; $id=$_GET['id']; ?> <?php if(isset($_POST['submit'])){ $name=$_POST['name']; $num=$_POST['num']; $classname=$_POST['classname']; $sex=$_POST['sex']; echo $name,$num,$classname,$sex,$id; if(trim($name)==''||trim($num)==''){ echo "你咋这么坏呢"; exit; } $sql="update student set name='$name',num='$num',c_id=$classname,sex='$sex' where id = $id"; $rs=mysql_query($sql); if(!$rs) echo '学号重复!'; else echo '修改成功!'; header('location:admin.php?titleid='.$titleid); } ?> <?php $sql='select * from class'; $rs=mysql_query($sql); ?> <form method='post' action='' onsubmit="return gaga()"> <table> <tr> <th>姓名:</th> <td><input type='text' name='name' id='name' value='<?php echo $name?>'></td> </tr> <tr> <th>学号:</th> <td><input type='text' name='num' id='num' value='<?php echo $num?>'></td> </tr> <tr> <th>班级:</th> <td><select name='classname' id='classname'> <?php while($cs=mysql_fetch_assoc($rs)):?> <option value='<?php echo $cs['id']?>' <?php if($cs['id']==$titleid) echo 'selected'?>><?php echo $cs['classname']?></option> <?php endwhile?> </select></td> </tr> <tr> <th>性别:</th> <td><input type='radio' name='sex' value='男' <?php if($sex=='男')echo 'checked' ?>>男 | <input type='radio' name='sex' value='女' <?php if($sex=='女')echo 'checked' ?>>女 </td> </tr> <tr> <th>操作:</th> <td><input type='submit' name='submit' value='修改'>|<input type='reset' name='submit' value='重置'></td> </tr> </table> </form> </body> </html>
做的有点不合理,代码写的一团糟 ,但是一也不影响我的追求极致心态 加油!加油! 再加油!!! ----致自己