显示info人员表里所有内容
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 </head> 7 8 <body> 9 <!--显示出text里面的info表全部内容数据--> 10 <table width="100%" border="1" cellpadding="0" cellspacing="0"> 11 <tr> 12 <td>代号</td> 13 <td>姓名</td> 14 <td>性别</td> 15 <td>名族</td> 16 <td>生日</td> 17 <td>操作</td> 18 </tr> 19 <!--要显示数据就要插入<?php查询数据库读取数据--> 20 <?php 21 //造连接对象 22 $db = new MySQLi("localhost","root","511108","text"); 23 //写SQL语句 24 $sql = "select * from info"; 25 //执行 26 $result = $db->query($sql); 27 $attr = $result->fetch_all(); 28 foreach($attr as $v) 29 { 30 $sex = $v[2];//处理性别的变量info表里的$sex名变男或女判断 31 $sex = $sex?"男":"女"; 32 33 //带有外键关系的分两步 34 //1.取出民族代号 35 $nation = $v[3]; 36 //2.对民族进行操作在做一个查询根据代号差名称 37 //造连接对象,由于上面有连接对象了所以这里可以直接用,但是SQL语句重名了和上面的所以要改下 38 /*$db = new MySQLi("localhost","root","511108","text");*/ 39 $sqln = "select name from nation where code='{$nation}'";//通过name一列查找代号nation民族 40 $rn = $db->query($sqln); 41 $an = $rn->fetch_row(); 42 43 44 echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$sex}</td><td>{$an[0]}</td><td>{$v[4]}</td><td><a href='xiugaiinfo1.php?code={$v[0]}'>修改</a></td></tr>";//$an[0]数组里面去索引0就是民族名称 45 } //<a href=要想修改'xiugaiinfo1.php就要传个主键值过去?code={$v[0]}'>修改</a> 46 47 48 ?> 49 50 </table> 51 52 </body> 53 </html>
修改info1表
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 </head> 7 8 <body> 9 <h1>修改人员信息</h1> 10 <form action="xiugairenyuanxinxichuli.php" method="post"> 11 <div> 12 <input type="hidden" value="" name="code" /> 13 </div> 14 <div>姓名:<input type="text" value="" name="name" /></div> 15 <div>性别:<input type="text" value="" name="sex" /></div> 16 <div>民族:<input type="text" value="" name="nation" /></div> 17 <div>生日:<input type="text" value="" name="birthday" /></div> 18 <div><input type="submit" value="修改" /></div> 19 20 21 22 23 </form>
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 </head> 7 8 <body> 9 <?php 10 //造链接对象 11 $code = $_GET["code"]; 12 $db = new MySQLi("localhost","root","511108","text"); 13 //写SQL语句 14 $sql = "select * from info where code='{$code}'"; 15 //执行 16 $result = $db->query($sql); 17 //取数据 18 $attr = $result->fetch_row(); 19 20 21 ?> 22 <!--在value代号里面嵌入<?php echo$attr[0] ?>默认里面的信息--> 23 <h1>修改人员信息 24 </h1><form action="xiugairenyuanxinxichuli.php" method="post"> 25 <div><input type="hidden" value="<?php echo$attr[0] ?>" name="code" /></div><!--代号隐藏--> 26 <div>姓名:<input type="text" value="<?php echo$attr[1] ?>" name="name" /></div> 27 <div>性别:<input type="text" value="<?php echo$attr[2] ?>" name="sex" /></div> 28 <div>民族:<input type="text" value="<?php echo$attr[3] ?>" name="nation" /></div> 29 <div>生日:<input type="text" value="<?php echo$attr[4] ?>" name="birthday" /></div> 30 <div><input type="submit" value="修改" /></div> 31 32 33 34 35 </form> 36 37 38 </body> 39 </html>
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 </head> 7 8 <body> 9 <?php 10 //造链接对象 11 $code = $_GET["code"]; 12 $db = new MySQLi("localhost","root","511108","text"); 13 //写SQL语句 14 $sql = "select * from info where code='{$code}'"; 15 //执行 16 $result = $db->query($sql); 17 //取数据 18 $attr = $result->fetch_row(); 19 20 21 ?> 22 <!--在value代号里面嵌入<?php echo$attr[0] ?>默认里面的信息--> 23 <!--<h1>修改人员信息</h1> 24 </h1><form action="xiugairenyuanxinxichuli.php" method="post"> 25 <div><input type="hidden" value="<?php echo$attr[0] ?>" name="code" /></div><!--代号隐藏--> 26 <!--<div>姓名:<input type="text" value="<?php echo$attr[1] ?>" name="name" /></div> 27 <div>性别:<input type="text" value="<?php echo$attr[2] ?>" name="sex" /></div> 28 <div>民族:<input type="text" value="<?php echo$attr[3] ?>" name="nation" /></div> 29 <div>生日:<input type="text" value="<?php echo$attr[4] ?>" name="birthday" /></div> 30 <div><input type="submit" value="修改" /></div>--> 31 32 33 34 <h1>修改人员信息</h1> 35 <form action="xiugairenyuanxinxichuli.php" method="post"> 36 <div><input type="hidden" value="<?php echo $attr[0] ?>" name="code" /></div><!--代号隐藏--> 37 <div>姓名:<input type="text" name="name" value="<?php echo $attr[1]?>" /></div> 38 <!--把性别的输入框可以换成单选按钮因为性别不可以全选--> 39 <div> 40 性别: 41 <input <?php echo $attr[2]?"checked='checked'":""; ?> type="radio" name="sex" value="1" />男<!--如果我想让男默认选中就在input后面按空格加checked="checcked"就可以了--> 42 <input <?php echo $attr[2]?"":"checked='checked'"; ?> type="radio" name="sex" value="0" />女 43 </div> 44 <!--民族输入框可以换成下拉列表完了把所有民族列出来--> 45 <div> 46 民族: 47 <select name="nation"> 48 <?php 49 //上面有SQL语句所以可以直接用就好改下名字要不冲突 50 $sqln = "select * from nation"; 51 $rn = $db->query($sqln);//调用SQL语句方法来用 52 $an = $rn->fetch_all();//用$rn->fetch_all()方法取所有民族、这是个数组 53 foreach($an as $vn) 54 { 55 if($attr[3]==$vn[0])//$attr[3]就只是 最外层的民族代号 56 { 57 echo "<option selected='selected' value='{$vn[0]}'>{$vn[1]}</option>"; 58 } 59 else 60 { 61 echo "<option value='{$vn[0]}'>{$vn[1]}</option>"; 62 } 63 } 64 ?> 65 </select> 66 </div> 67 <div>生日:<input type="text" value="<?php echo $attr[4] ?>" name="birthday" /></div> 68 <div><input type="submit" value="修改" /></div> 69 70 </form> 71 72 73 </body> 74 </html>
1 <?php 2 $code = $_POST["code"];//代号 3 $name = $_POST["name"]; 4 $sex = $_POST["sex"];//性别 5 $nation = $_POST["nation"];//民族代号 6 $birthday = $_POST["birthday"];//生日 7 8 $db = new MySQLi("localhost","root","511108","text"); 9 //写SQL语句,这是个修改语句 10 $sql = "update info set name='{$name}',sex={$sex},nation='{$nation}',birthday='{$birthday}' where code='{$code}'"; 11 12 //echo $sql;//输出 13 14 $r = $db->query($sql); 15 16 if($r) 17 { 18 header("location:inforenyuan.php"); 19 } 20 else 21 { 22 echo "修改失败!"; 23 }