列表页代码:
<?php //连接数据库 $db = new MySQLi('localhost','root','','z_1032'); !mysql_connect_error() or die('连接失败'); $db->query('set names utf8'); //sql条件 $where = 'where 1=1'; if(!empty($_POST['name'])){ $name = $_POST['name']; $where .= " and name like '%$name%'"; } if(!empty($_POST['tel'])){ $tel = $_POST['tel']; $where .= " and tel = '$tel'"; } //查询数据 $sql = "select * from lxr_lianxiren where 1 = 1 ".$where; $res = $db->query($sql);//执行sql语句 $arr = $res->fetch_all();//结果集返回数组,索引数组 //查询数据 $sql = "select * from lxr_groups"; $res = $db->query($sql); $attr = array(); while($row=$res->fetch_assor()){//fetch_assor()返回一行数据 关联数组 $attr[$row['id']] = $row['name']; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>列表页</title> </head> <body> <a href="add.php"><button>添加</button></a> <form action="list.php" method="post"> 姓名: <input type="text" name="name"> 手机号: <input type="text" name="tel"> <button>查询</button> </form> <table width="80%" border="1" cellpadding="0" cellspacing="0"> <tr> <th>id</th> <th>姓名</th> <th>手机号</th> <th>分组</th> <th>操作</th> </tr> <?php foreach($arr as $v){ ?> <tr> <td><?php echo $v[0]; ?></td> <td><?php echo $v[1]; ?></td> <td><?php echo $v[2]; ?></td> <td><?php echo $attr[$v[3]]; ?></td> <td> <a href="php.php?<?php echo $v[0]; ?>"> <button>删除</button> </a> <a href="edit.php?<?php echo $v[0]; ?>"> <button>修改</button> </a> </td> </tr> <?php } ?> </table> </body> </html>
添加页代码:
<?php //连接数据库 $db = new MySQLi('localhost','root','','z_1032'); !mysql_connect_error() or die('连接失败'); $db->query('set names utf8'); //查询数据 $sql = "select * from lxr_groups"; $res = $db->query($sql); $attr = array(); while($row=$res->fetch_assor()){//fetch_assor()返回一行数据 关联数组 $attr[$row['id']] = $row['name']; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <form action="php.php?type=add" method="post"> 联系人: <input type="text" name="name"> <br> tel: <input type="text" name="tel"> <br> 分组: <select name="groupid" id=""> <?php foreach($attr as $k=>$v){ ?> <option value="<?php echo $k; ?>"><?php echo $v; ?></option> <?php } ?> </select> <br> <button>提交</button> </form> </body> </html>
修改页代码:
<?php //连接数据库 $db = new MySQLi('localhost','root','','z_1032'); !mysql_connect_error() or die('连接失败'); $db->query('set names utf8'); $id = $_GET['id']; //查询数据 $sql = "select * from lxr_lianxiren where id = $id"; $res = $db->query($sql); $arr=$res->fetch_assor(); //查询数据 $sql = "select * from lxr_groups"; $res = $db->query($sql); $attr = array(); while($row=$res->fetch_assor()){//fetch_assor()返回一行数据 关联数组 $attr[$row['id']] = $row['name']; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <form action="php.php?type=update" method="post"> <input type="hidden" name="id" value="<?php echo $id; ?>"> 联系人: <input type="text" name="name" value="<?php echo $arr['name']; ?>"> <br> tel: <input type="text" name="tel" value="<?php $arr['tel']; ?>"> <br> 分组: <select name="groupid" id=""> <?php foreach($attr as $k=>$v){ if($k == $arr['groupid']){ echo "<option value='$k' selected>$v</option>"; }else{ echo "<option value='$k'>$v</option>"; } } ?> </select> <br> <button>提交</button> </form> </body> </html>
后台php代码:
<?php //连接数据库 $db = new MySQLi('localhost','root','','z_1032'); !mysql_connect_error() or die('连接失败'); $db->query('set names utf8'); $type = $_REQUEST['type'];//通过这个类型判断修改还是删除 switch($type){ case 'update': $id = $_POST['id']; $name = $_POST['name']; $tel = $_POST['tel']; $groupid = $_POST['groupid']; $sql = "update lxr_lianxiren set name = '$name', tel = '$tel',groupid = '$groupid' where id = $id"; $res = $db->query($sql); if($res){ header('localhost:list.php');//header()页面跳转 }else{ echo "修改失败"; header('refresh:3;url=list.php?id=10'); } break; break; case 'add': $name = $_POST['name']; $tel = $_POST['tel']; $groupid = $_POST['groupid']; $sql = "insert into lxr_lianxiren(name,tel,groupid) values('$name','$tel','$groupid')"; $res = $db->query($sql); if($res){ header('localhost:list.php');//header()页面跳转 }else{ echo "添加失败"; header('refresh:3;url=list.php?id=10'); } break; default: //接收值 $id = $_GET['id']; //执行sql语句,删数据 $sql = "delete from lxr_lianxiren where id = $id"; $res = $db->query($sql); if($res){ header('localhost:list.php');//header()页面跳转 }else{ echo "删除失败"; header('refresh:3;url=list.php?id=10'); } break; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> </body> </html>