标签:
要求见文件-内测:联系人管理.pdf
数据库为mycontacts
表格为contacts,groups
表格内容为:
zhuye.php
<!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> <div> <?php $db= new mysqli("localhost","root","123","Mycontacts"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql= "select contacts.id,contacts.Name,contacts.Tel,Groups.Name from contacts,Groups where contacts.GroupId=Groups.id"; $result=$db->query($sql); echo "<table cellpadding=‘0‘ cellspacing=‘0‘ border=‘1‘ style=‘text-align:center‘>"; echo "<tr height=‘40px‘><td width=‘100px‘></td> <td width=‘60px‘>姓名</td> <td width=‘100px‘>电话</td> <td width=‘50px‘>分组</td> </tr>"; while($row=$result->fetch_row()) { echo "<tr height=‘40px‘><td><a href=‘xiugai.php?code=".$row[0]."‘>编辑</a> <a href=‘delete.php?code=".$row[0]."‘ onclick=\"return confirm(‘确定删除吗?‘)\">删除</a></td> <td>{$row[1]}</td> <td>{$row[2]}</td> <td>{$row[3]}</td> </tr>"; } echo "</table>"; } ?> </div> <div> <form> <input type="submit" value="添加新号码" name="alert" onclick="window.open(‘alert.php‘)" /> </form> </div> </body> </html>
alert.php
<!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> <div> <?php $db= new mysqli("localhost","root","123","Mycontacts"); if(mysqli_connect_error()) { echo "连接错误"; } else { $sql= "select contacts.id,contacts.Name,contacts.Tel,Groups.Name from contacts,Groups where contacts.GroupId=Groups.id"; $result=$db->query($sql); echo "<table cellpadding=‘0‘ cellspacing=‘0‘ border=‘1‘ style=‘text-align:center‘>"; echo "<tr height=‘40px‘><td width=‘100px‘></td> <td width=‘60px‘>姓名</td> <td width=‘100px‘>电话</td> <td width=‘50px‘>分组</td> </tr>"; while($row=$result->fetch_row()) { echo "<tr height=‘40px‘><td><a href=‘xiugai.php?code=".$row[0]."‘>编辑</a> <a href=‘delete.php?code=".$row[0]."‘ onclick=\"return confirm(‘确定删除?‘)\">删除</a></td> <td>{$row[1]}</td> <td>{$row[2]}</td> <td>{$row[3]}</td> </tr>"; } echo "</table>"; } ?> </div> <br /> <div> <form action="tianjiacl.php" method="post"> 姓名:<input type="text" name="name" /><br /><br /> 电话:<input type="text" name="tel" /><br /><br /> 分组: <select name="groups"> <?php //1.连接数据库 $db= new mysqli("localhost","root","123","Mycontacts"); //2.判断是否连接成功 if(mysqli_connect_error()) { echo "连接失败"; } else { //3.写sql语句 $sql="select * from Groups"; //4.执行sql语句 $result=$db->query($sql); //5.处理数据,遍历 while($row=$result->fetch_row()) { echo "<option value=‘{$row[0]}‘>{$row[1]}</option>"; } } ?> </select> <br /> <div><input type="submit" value="添加"/></div> <br /> <div><a href="zhuye.php">返回首页</a></div> </form> </div> </body> </html>
tianjiacl.php
<!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> <?php $name = $_POST["name"]; $tel = $_POST["tel"]; $groups = $_POST["groups"]; //1.造连接对象 $db= new mysqli("localhost","root","123","Mycontacts"); //2.判断是否连接 if(mysqli_connect_error()) { echo "连接错误"; } else { //3.写添加语句 $sql="insert into contacts values(‘‘,‘{$name}‘,‘{$tel}‘,‘{$groups}‘)"; //4.执行语句 $result=$db->query($sql); //5.判断是否添加成功 if($result) { header("Location:alert.php");//跳回页面 } else { echo "添加失败"; } } ?> </body> </html>
delete.php
<!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> <?php $code = $_GET["code"]; //1.造连接对象 $db=new mysqli("localhost","root","123","Mycontacts"); //2.判断连接是否成功 if(mysqli_connect_error()) { echo "连接错误"; } else { //3.写sql语句 $sql="delete from contacts where id=‘".$code."‘"; //4.执行sql语句 $result=$db->query($sql); //判断是否删除成功 if($result) { header("Location:zhuye.php"); } else { echo "删除失败!"; } } ?> </body> </html>
xiugai.php
<!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> <?php $code = $_GET["code"]; //1.造连接对象 $db = new mysqli("localhost","root","123","Mycontacts"); //2.判断是否连接成功 if(mysqli_connect_error()) { echo "连接失败"; } else { //3.写sql语句 $sql= "select contacts.id,contacts.Name,contacts.Tel,Groups.Name from contacts,Groups where contacts.GroupId=Groups.id and contacts.id=‘".$code."‘"; //4.执行sql语句 $result=$db->query($sql); //5.处理数据 $row=$result->fetch_row(); } ?> <form action="update.php" method="post"> <input type="text" name="id" value="<?php echo $row[0]?>" style="visibility:hidden" /> <div>姓名:<input type="text" id="xingming" name="name" value="<?php echo $row[1]?>" /></div><br /> <div>电话:<input type="text" id="tel" name="tel" value="<?php echo $row[2] ?>" /></div><br /> <div> 分组: <select name="groups"> <?php //1.造连接对象 $db=new mysqli("localhost","root","123","Mycontacts"); //2.判断连接是否成功 if(mysqli_connect_error()) { echo "连接失败"; } else { //3.写sql语句 $sql="select * from groups"; //4.执行sql语句 $result=$db->query($sql); //5.处理数据 //遍历数据 while($rowgroups=$result->fetch_row()) { if($rowgroups[0]==$row[3]) { echo "<option selected=‘selected‘ value=‘{$rowgroups[0]}‘>{$rowgroups[1]}</option>"; } else { echo "<option value=‘{$rowgroups[0]}‘>{$rowgroups[1]}</option>"; } } } ?> </select> </div><br /> <div><input type="submit" value="修改" /> <a href="zhuye.php">返回</a></div> </form> </body> </html> <script> var a=document.getElementById("xingming").value; if(a=="") alert("非空"); </script>
update.php
<!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> <?php $id=$_POST["id"]; $name=$_POST["name"]; $tel=$_POST["tel"]; $groups=$_POST["groups"]; //1.造连接对象 $db=new mysqli("localhost","root","123","Mycontacts"); //2.判断是否连接成功 if(mysqli_connect_error()) { echo "连接失败"; } else { //3.写语句 $sql="update contacts set name=‘".$name."‘,tel=‘".$tel."‘,groupid=‘".$groups."‘where id=‘".$id."‘"; //4.执行sql语句 $result = $db->query($sql); //判断是否修改成功 if($result) { header("Location:zhuye.php"); } else { echo "修改失败!"; } } ?> </body> </html>
标签:
原文地址:http://www.cnblogs.com/Chenshuai7/p/5196848.html