1.建test.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> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr bgcolor="#33FFCC"> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td> </tr> <?php //造连接对象 $db = new MySQLi("localhost","root","1234","testa"); //判断连接是否成功 !mysqli_connect_error() or die("连接失败!"); //写SQL语句 $sql = "select * from Info"; //执行SQL语句 $result = $db->query($sql); //处理查询的结果 $attr = $result->fetch_all(); for($i=0;$i<count($attr);$i++) { echo "<tr>"; for($j=0;$j<count($attr[$i]);$j++) { echo "<td>{$attr[$i][$j]}</td>"; } echo "<td><a href='Delete.php?code={$attr[$i][0]}'>删除</a><a href='Update.php?code={$attr[$i][0]}'>修改</a></td>"; echo "</tr>"; } ?> </table> <br /> <a href="Add.php"><input type="button" value="添加数据" /></a> </body> </html>
2.建立Add.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> <form action="Addchuli.php" method="post"> <div> 代号: <input type="text" name="code" /> </div> <div> 姓名: <input type="text" name="name" /> </div> <div> 性别: 男<input type="radio" value="true" name="sex" checked="checked" /> 女<input type="radio" value="false" name="sex" /> </div> <div> 民族: <select name="nation"> <?php //造连接对象 $db = new mysqli("localhost","root","1234","testa"); //判断是否出错 !mysqli_connect_error() or die("连接失败!"); //写SQL语句 $sql = "select * from Nation"; //执行SQL语句 $result = $db->query($sql); //处理结果 $attr = $result->fetch_all(); for($i=0;$i<count($attr);$i++) { echo "<option value='{$attr[$i][0]}'>{$attr[$i][1]}</option>"; } ?> </select> </div> <div> 生日: <input type="text" name="birthday" /> </div> <div> <input type="submit" value="确定" /> <a href="test.php">返回主页</a> </div> </form> </body> </html>
3.建立Addchuli.php用于处理Add.php‘添加数据’的处理
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; //造连接对象 $db = new mysqli("localhost","root","1234","testa"); //判断是否出错 !mysqli_connect_error() or die("连接失败!"); //写SQL语句 $sql = "insert into Info values('{$code}','{$name}',{$sex},'{$nation}','{$birthday}')"; //执行SQL语句 $result = $db->query($sql); if($result) { header("location:Add.php"); } else { echo "执行失败!"; }
4.建立删除文件Delete.php
<?php $code = $_GET["code"]; //造连接对象 $db = new mysqli("localhost","root","1234","testa"); //判断是否出错 !mysqli_connect_error() or die("连接失败!"); //写SQL语句 $sql = "delete from Info where Code = '{$code}'"; //执行SQL语句 $result = $db->query($sql); if($result) { header("location:test.php"); } else { echo "删除失败!"; }
5.建立修改文件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 $code = $_GET["code"]; //造连接对象 $db = new mysqli("localhost","root","1234","testa"); //判断是否出错 !mysqli_connect_error() or die("连接失败!"); //写SQL语句 $sqlxq = "select * from Info where Code = '{$code}'"; //执行SQL语句 $resultxq = $db->query($sqlxq); $attrxq = $resultxq->fetch_row(); ?> <form action="UpdateChuLi.php" method="post"> <div> 代号: <input readonly="readonly" type="text" name="code" value="<?php echo $attrxq[0]; ?>" /> </div> <div> 姓名: <input type="text" name="name" value="<?php echo $attrxq[1]; ?>" /> </div> <div> 性别: 男<input type="radio" value="true" name="sex" <?php echo $attrxq[2]?"checked='checked'":""; ?> /> 女<input type="radio" value="false" name="sex" <?php echo $attrxq[2]?"":"checked='checked'"; ?> /> </div> <div> 民族: <select name="nation"> <?php //写SQL语句 $sql = "select * from Nation"; //执行SQL语句 $result = $db->query($sql); //处理结果 $attr = $result->fetch_all(); for($i=0;$i<count($attr);$i++) { if($attrxq[3] == $attr[$i][0]) { echo "<option selected='selected' value='{$attr[$i][0]}'>{$attr[$i][1]}</option>"; } else { echo "<option value='{$attr[$i][0]}'>{$attr[$i][1]}</option>"; } } ?> </select> </div> <div> 生日: <input type="text" name="birthday" value="<?php echo $attrxq[4]; ?>" /> </div> <div> <input type="submit" value="修改" /> <a href="test.php">返回主页</a> </div> </form> </body> </html>
6.建立修改信息的UpdateChuLi.php文件
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; //造连接对象 $db = new mysqli("localhost","root","1234","testa"); //判断是否出错 !mysqli_connect_error() or die("连接失败!"); //写SQL语句 $sql = "update Info set Name='{$name}',Sex={$sex},Nation='{$nation}',Birthday='{$birthday}' where Code = '{$code}'"; //执行SQL语句 $result = $db->query($sql); if($result) { header("location:test.php"); } else { echo "修改失败!"; }