通讯录管理系统 准备工具:Atom,Wampserver,navicat,chrome 首先是创建数据库创建表,这里取名数据库名mtdb,表名list 字段为Num学号,Name姓名,Sex性别,Birthday生日,QQqq,Mobile手机,Email邮箱,Address地址
连接数据库 <?php //连接数据库 error_reporting(E_ALL ^ E_DEPRECATED); //错误控制 header("Content-type:text/html;charset=utf-8"); //客户端发送原始的 HTTP 报头 $conn=mysql_connect('localhost','root',''); //连接MySQL服务器 mysql_select_db('mydb',$conn); //选择数据库 mysql_query("SET NAMES utf8"); //设置字符集为utf8 session_start(); //启动session会话 ?> 主页( 主页比较简单,用html来写的) <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>通讯录管理系统</title> <link href="style/index.css" rel="stylesheet" /> </head> <body> <table id="main" border="1" bordercolor="#CC6633" cellspacing="0"> <caption id="head">通讯录管理系统</caption> <tr id="nav"> <td><a href="display.php">显示通讯录</a></td> <td rowspan="3"><img src="images/bg.jpg" width="510" height="290"/></td> </tr> <tr id="nav"> <td><a href="add.php">添加通讯录</a></td> </tr> <tr id="nav"> <td><a href="search.php">查找通讯录</a></td> </tr> </table> </body> </html> 添加数据库 (使用正则表达式,所有信息不能为空且学号、手机和邮箱有限制。当只有正则表达式与字符串完全匹配,点击提交将数据添加到数据库) <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>添加通讯录</title> <link href="style/add.css" rel="stylesheet" /> </head> <body> <h2 align="center">添加通讯录</h2> <?php if (isset($_REQUEST["BUTTON1"])) { $con=@mysql_connect('localhost','root',''); //连接服务器 mysql_select_db('mydb', $con); //选择数据库 mysql_query("set names utf8");//设置字符集为utf8 $test=1; //只要$test=0,则表单信息就无法提交 $num = $_REQUEST['num']; $name = $_REQUEST['name']; $sex = @$_REQUEST['sex']; $birthday = $_REQUEST['birthday']; $qq = $_REQUEST['qq']; $mobile = $_REQUEST['mobile']; $email = $_REQUEST['email']; $address = $_REQUEST['address']; //若正则表达式含^、$,只有正则表达式与字符串完全匹配,该函数才返回1。 if($num == ''){$xh1="学号不能为空";$test = 0;} elseif(preg_match('/^2018\d{8}$/',$num) == 0){$xh1="学号为2018开头的12位数字";$test=0;} if($name == ''){$xm1="姓名不能为空";$test = 0;} if($sex == ''){$xb1="性别不能为空";$test = 0;} if($birthday == ''){$sr1="生日不能为空";$test = 0;} if($qq == ''){$qq1="QQ不能为空";$test = 0;} if($mobile == ''){$phone1="电话不能为空";$test = 0;} elseif(preg_match('/^1\d{10}$/',$mobile) == 0){$phone1="电话必须是以1开头的11位数字";$test=0;} if($email == ''){$email1="邮箱不能为空";$test = 0;} elseif(preg_match('/^[\w_\-]+@[\w_]+\.[\w_\.]+$/',$email) == 0){$email1="邮箱必须是XXX@XXX.XXX";$test=0;} if($address == ''){$address1="地址不能为空";$test = 0;} if ($test==1) { $sql="insert into list values('$num','$name','$sex','$birthday','$qq','$mobile','$email','$address')"; $addresslist=mysql_query($sql); if ($addresslist==true) echo "<script language='javascript'>window.alert('插入成功!');location.href='display.php';</script>"; else echo "<script language='javascript'>window.alert('插入失败');</script>"; } } ?> <form id="form2" name="form2" method="post" action=""> <table width="500" height="500" border="1" align="center" bordercolor="#DDDDDD"> <tr> <td width="50">学号</td> <td width="250"><label> <input name="num" type="text" id="num" /> </label><?php echo @$xh1; ?></td> </tr> <tr> <td>姓名</td> <td><label> <input name="name" type="text" id="name" /> </label><?php echo @$xm1; ?></td> </tr> <tr> <td>性别</td> <td><label> <input type="radio" name="sex" id="sex" value="男" /> 男 <input type="radio" name="sex" id="sex" value="女" /> 女 </label><?php echo @$xb1; ?></td> </tr> <tr> <td>生日</td> <td><label> <input name="birthday" type="date" id="birthday" /> </label><?php echo @$sr1; ?></td> </tr> <tr> <td>QQ</td> <td><label> <input name="qq" type="text" id="qq" /> </label><?php echo @$qq1; ?></td> </tr> <tr> <td>手机</td> <td><label> <input name="mobile" type="text" id="mobile" /> </label><?php echo @$phone1; ?></td> </tr> <tr> <td>邮箱</td> <td><label> <input name="email" type="text" id="email" /> </label><?php echo @$email1; ?></td> </tr> <tr> <td>地址</td> <td><label> <input name="address" type="text" id="address" size="40" /> </label><?php echo @$address1; ?></td> </tr> <tr> <td colspan="2"><label> <div align="center"> <input type="submit" name="BUTTON1" value="提交" /> <input type="reset" name="BUTTON2" value="重置" /> </div> </label></td> </tr> </table> </form> <div class="back"> <a href="index.php">返回</a> </div> <?php ?> </body> </html> 显示数据库 (将数据库所有信息显示出来,分页进行显示,也可删除,修改暂时没弄好) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>显示通讯录</title> <link href="style/display.css" rel="stylesheet"> </head> <body> <?php $con=@mysql_connect('localhost','root','');//连接数据库 mysql_select_db('mydb');//选择数据库 mysql_query("set names utf8"); //设置字符集为utf8 $pagesize=10; //定义显示数据为10条 $result=mysql_query('select count(*) from list'); $rows=mysql_fetch_row($result); $recordcount=$rows[0]; //总记录数 $pagecount=ceil($recordcount/$pagesize); //总页数 $pageno=isset($_GET['pageno'])?$_GET['pageno']:1; //获得当前页面 $starno=($pageno-1)*$pagesize; $sql = "select * from list limit $starno,$pagesize";//获得当前页面的内容 $result=mysql_query($sql); ?> <h2 align="center">我的通讯录</h2> <table width="90%" border="1" align="center" > <tr> <th bgcolor="#FF6666" scope="col">学号</th> <th bgcolor="#FF6666" scope="col">姓名</th> <th bgcolor="#FF6666" scope="col">性别</th> <th bgcolor="#FF6666" scope="col">生日</th> <th bgcolor="#FF6666" scope="col">QQ</th> <th bgcolor="#FF6666" scope="col">手机</th> <th bgcolor="#FF6666" scope="col">邮箱</th> <th bgcolor="#FF6666" scope="col">地址</th> </tr> <?php $row=mysql_fetch_row($result); while($row) { ?> <tr> <td height="30" align="center"><?php echo $row[0];?></td> <td height="30" align="center"><?php echo $row[1];?></td> <td height="30" align="center"><?php echo $row[2];?></td> <td height="30" align="center"><?php echo $row[3];?></td> <td height="30" align="center"><?php echo $row[4];?></td> <td height="30" align="center"><?php echo $row[5];?></td> <td height="30" align="center"><?php echo $row[6];?></td> <td height="30" align="center"><?php echo $row[7];?></td> <td> <a href="modify.php?num=<?php echo $row[0];?>"><input type="button" value="修改"></a> <!-- <input type="button" value="修改" onclick="location.href=\'modify.php?num='.$row[0].'\'" /> --> <a onclick="return confirm('你确定删除吗?')" href="delete.php?num=<?php echo $row[0];?>"><input type="button" value="删除"></a> </td> </tr> <?php $row=mysql_fetch_row($result); } ?> </table> <table align="center"> <td> <a href="display.php?pageno=1">首页</a> <a href="display.php?pageno=<?php echo $pageno-1; ?>">上一页</a> <?php //循环输出页码 for($i=1;$i<=$pagecount;$i++) { echo '<a href="search.php?pageno='.$i.'">'.$i.'</a> '; } ?> <a href="display.php?pageno=<?php echo $pageno+1; ?>">下一页</a> <a href="display.php?pageno=<?php echo $pagecount; ?>">尾页</a> 第<?php echo $pageno;?>页/共<?php echo $pagecount;?>页 </td> </table> <div class="back"> <a href="add.php">添加</a> <a href="index.php">返回</a> </div> </body> </html> 查询数据库(按姓名进行查找) 在这里插入代码片<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>查询联系人</title> <link rel="stylesheet" href="style/search.css"> </head> <body> <?php $con=@mysql_connect('localhost','root',''); //连接服务器 mysql_select_db('mydb', $con); //选择数据库 mysql_query("set names utf8");//设置字符集为utf8 // $Num=$_REQUEST['Num']; // $sql="select * from addresslist where Num='$Num'";//查询 $Name=$_REQUEST['Name']; $sql="select * from list where Name like '%$Name%'";//查询 $result=mysql_query($sql); if(!$result){ die('无法读取数据:'.mysql_error()); } ?> <h2 align="center">查询通讯录</h2> <form id="form" align="center" name="form" method="post" action=""> <!-- <label> 学号:<input name="Num" type="text" id="Num" placeholder="请输入学号" /> </label> --> <label> 姓名:<input name="Name" type="text" id="Name" placeholder="请输入姓名"/> </label> <label> <input type="submit" name="search" value="查询" /> </label> </form> <br> <table width="90%" border="1" align="center" > <tr> <th bgcolor="#FF6666" scope="col">学号</th> <th bgcolor="#FF6666" scope="col">姓名</th> <th bgcolor="#FF6666" scope="col">性别</th> <th bgcolor="#FF6666" scope="col">生日</th> <th bgcolor="#FF6666" scope="col">QQ</th> <th bgcolor="#FF6666" scope="col">手机</th> <th bgcolor="#FF6666" scope="col">邮箱</th> <th bgcolor="#FF6666" scope="col">地址</th> </tr> <?php $row=mysql_fetch_row($result); while($row) { ?> <tr> <td height="30" align="center"><?php echo $row[0];?></td> <td height="30" align="center"><?php echo $row[1];?></td> <td height="30" align="center"><?php echo $row[2];?></td> <td height="30" align="center"><?php echo $row[3];?></td> <td height="30" align="center"><?php echo $row[4];?></td> <td height="30" align="center"><?php echo $row[5];?></td> <td height="30" align="center"><?php echo $row[6];?></td> <td height="30" align="center"><?php echo $row[7];?></td> </tr> <?php $row=mysql_fetch_row($result); } ?> </table> <div class="back"> <a href="index.php">返回</a> </div> </body> </html> 删除数据库 <meta charset="utf-8"> <?php $num = $_GET["num"]; $db = new mysqli("localhost","root","","mydb"); $sql = "delete from list where Num = '{$num}'";//通过Num查询 if($db->query($sql))//执行sql语句 { header("location:display.php"); } else { echo "删除失败"; } ?>界面大概是这样: 主页界面 添加通讯录界面
显示通讯录界面 查找通讯录界面