主页面——0126.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> <h1>主页面</h1> <div style="width:100%"> <?php //1.生成连接 造连接对象 //$db=new mysqli($dbhost(服务器),$username,$userpass,$dbdatabase); $db = new mysqli("localhost","root","123","mydb"); //2.判断是否连接成功 成功为空,失败返回string(字符串)错误信息 if(mysqli_connect_error()) { echo "连接失败"; //exit 退出整个程序 } else { //3.写sql语句 $sql = "select * from Info"; //4.执行sql语句 $result=$db->query($sql); //5.处理数据(提取数据) echo "<table width=90% cellpadding='0' cellspacing='0' border='1'>"; echo "<tr><td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> <td>操作</td></tr>"; //遍历每一条数据 while($row=$result->fetch_row()) { //处理性别 $sex=(bool)$row[2]?"男":"女"; //处理民族 $nation=NationName($db,$row[3]);//调方法不加$ //处理生日 $birthday=date("Y年m月d日",strtotime($row[4])); echo "<tr bgcolor='#00CC33'><td>{$row[0]}</td> <td>{$row[1]}</td> <td>{$sex}</td> <td>{$nation}</td> <td>{$birthday}</td><td><a href='Delete.php?code=".$row[0]."'>删除</a> <a href='xiugai.php?code=".$row[0]."'>修改</a></td></tr>"; } echo "</table>"; } //根据民族代号查询民族名称 function NationName($db,$code) { //3.写sql语句 $sql = "select * from Nation where code='{$code}'"; //4.执行sql语句 $result=$db->query($sql); //处理数据 if($row=$result->fetch_row()) { return $row[1];//不加"" } else { return ""; } } ?> </div> <div><a href="ADD.php">添加数据</a> </div> </body> </html>
添加数据:
添加页面——ADD.php
<body> <h1>添加页面</h1> <form action="Insert.php" method="post"> <div><span>代号:</span><input type="text" name="code" /></div> <div><span>姓名:</span><input type="text" name="name"/></div> <div><span>性别:</span><input type="radio" name="sex" value="true" checked="checked"/>男 <input type="radio" name="sex" value="false"/>女</div> <div><span>民族:</span> <select name="nation"> <?php //1.连接数据库 $db= new mysqli("localhost","root","123","mydb"); //2.判断是否连接成功 if(mysqli_connect_error()) { echo "连接失败"; } else { //3.写sql语句 $sql="select * from nation"; //4.执行sql语句 $result=$db->query($sql); //5.处理数据,遍历 while($row=$result->fetch_row()) { echo "<option value='{$row[0]}'>{$row[1]}</option>"; } } ?> </select> </div> <div><span>生日:</span><input type="text" name="birthday"/></div> <div><input type="submit" value="添加"/> <a href="0126.php">返回</a></div> </form> </body>
添加数据——处理页面——insert.php
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; //1.造连接对象 $db= new mysqli("localhost","root","123","mydb"); //2.判断是否连接 if(mysqli_connect_error()) { echo "连接错误"; } else { //3.写添加语句 $sql="insert into Info values('{$code}','{$name}',{$sex},'{$nation}','{$birthday}')"; //4.执行语句 $result=$db->query($sql); //5.判断是否添加成功 if($result) { header("Location:ADD.php");//跳回页面 } else { echo "添加失败"; } } ?>
删除数据处理页面——delete.php
<?php $code = $_GET["code"]; //1.造连接对象 $db=new mysqli("localhost","root","123","mydb"); //2.判断连接是否成功 if(mysqli_connect_error()) { echo "连接错误"; } else { //3.写sql语句 $sql="delete from Info where code='".$code."'"; //4.执行sql语句 $result=$db->query($sql); //判断是否删除成功 if($result) { header("Location:0126.php"); } else { echo "删除失败!"; } } ?>
修改数据——修改页面——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> <h1>修改页面</h1> <?php $code = $_GET["code"]; //1.造连接对象 $db = new mysqli("localhost","root","123","mydb"); //2.判断是否连接成功 if(mysqli_connect_error()) { echo "连接失败"; } else { //3.写sql语句 $sql="select * from Info where code='".$code."'"; //4.执行sql语句 $result=$db->query($sql); //5.处理数据 $row=$result->fetch_row(); } ?> <form action="update.php" method="post"> <div><span>代号:</span><input type="text" name="code" value="<?php echo $row[0]?>" readonly="readonly" /></div> <div><span>姓名:</span><input type="text" name="name" value="<?php echo $row[1]?>"</div> <div> <span>性别:</span> <input type="radio" name="sex" value="true" <?php echo (bool)$row[2]?"checked='checked'":""?> />男 <input type="radio" name="sex" value="false" <?php echo !(bool)$row[2]?"checked='checked'":""?> />女 </div> <div> <span>民族:</span> <select name="nation"> <?php //1.造连接对象 $db=new mysqli("localhost","root","123","mydb"); //2.判断连接是否成功 if(mysqli_connect_error()) { echo "连接失败"; } else { //3.写sql语句 $sql="select * from nation"; //4.执行sql语句 $result=$db->query($sql); //5.处理数据 //遍历数据 while($rownation=$result->fetch_row()) { if($rownation[0]==$row[3]) { echo "<option selected='selected' value='{$rownation[0]}'>{$rownation[1]}</option>"; } else { echo "<option value='{$rownation[0]}'>{$rownation[1]}</option>"; } } } ?> </select> </div> <div><span>生日:</span><input type="text" name="birthday" value="<?php echo $row[4] ?>"</div> <div><input type="submit" value="修改" /> <a href="0126.php">返回</a></div> </form> </body> </html>
修改数据——处理页面——update.php
<?php $code=$_POST["code"]; $name=$_POST["name"]; $sex=$_POST["sex"]; $nation=$_POST["nation"]; $birthday=$_POST["birthday"]; //1.造连接对象 $db=new mysqli("localhost","root","123","mydb"); //2.判断是否连接成功 if(mysqli_connect_error()) { echo "连接失败"; } else { //3.写语句 $sql="update info set name='".$name."',sex='".$sex."',nation='".$nation."',birthday='".$birthday."'where code='".$code."'"; //4.执行sql语句 $result = $db->query($sql); //判断是否修改成功 if($result) { header("Location:0126.php"); } else { echo "修改失败!"; } } ?>