时间:2021-07-01 10:21:17 帮助过:3人阅读
添加页面
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 10 <h1>添加页面</h1> 11 12 <form action="Insert.php" method="post"> 13 14 <div><span style="width:80px;">代号:</span><input type="text" name="code" /></div> 15 16 <div><span style="width:80px;">姓名:</span><input type="text" name="name" /></div> 17 18 <div><span style="width:80px;">性别:</span><input type="radio" name="sex" value="true" checked="checked" />男 <input type="radio" name="sex" value="false"/>女</div> 19 20 <div><span style="width:80px;">民族:</span> 21 <select name="nation"> 22 <?php 23 //1.造连接对象 24 $db = new mysqli("localhost","root","123","mydb"); 25 26 //2.判断是否连接成功 27 if(mysqli_connect_error()) 28 { 29 echo "连接失败"; 30 31 //退出整个程序 32 exit; 33 } 34 else 35 { 36 //3.写SQL语句 37 $sql = "select * from Nation"; 38 39 //4.执行SQL语句 40 $result = $db->query($sql); 41 42 //5.处理数据 43 //遍历每一条数据 44 while($row = $result->fetch_row()) 45 { 46 echo "<option value=‘{$row[0]}‘>{$row[1]}</option>"; 47 } 48 } 49 ?> 50 </select> 51 </div> 52 53 <div><span style="width:80px;">生日:</span><input type="text" name="birthday" /></div> 54 55 <div><input type="submit" value="添加"/> <a href="Test.php">返回</a></div> 56 57 58 </form> 59 60 </body> 61 </html>
添加处理
1 <?php 2 3 $code = $_POST["code"]; 4 $name = $_POST["name"]; 5 $sex = $_POST["sex"]; 6 $nation = $_POST["nation"]; 7 $birthday = $_POST["birthday"]; 8 9 //1.造连接对象 10 $db = new mysqli("localhost","root","123","mydb"); 11 12 //2.判断是否连接成功 13 if(mysqli_connect_error()) 14 { 15 echo "连接失败"; 16 17 //退出整个程序 18 exit; 19 } 20 else 21 { 22 //3.写SQL语句 23 $sql = "insert into Info values(‘{$code}‘,‘{$name}‘,{$sex},‘{$nation}‘,‘{$birthday}‘)"; 24 25 26 //4.执行SQL语句 27 $result = $db->query($sql); 28 29 //判断是否添加成功 30 31 if($result) 32 { 33 header("Location:Add.php"); 34 } 35 else 36 { 37 echo "添加失败"; 38 } 39 } 40 41 ?>
修改页面
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 10 <h1>修改页面</h1> 11 12 <?php 13 14 $code = $_GET["code"]; 15 16 //1.造连接对象 17 $db = new mysqli("localhost","root","123","mydb"); 18 19 //2.判断是否连接成功 20 if(mysqli_connect_error()) 21 { 22 echo "连接失败"; 23 24 //退出整个程序 25 exit; 26 } 27 else 28 { 29 //3.写SQL语句 30 $sql = "select * from Info where Code=‘".$code."‘"; 31 32 //4.执行SQL语句 33 $result = $db->query($sql); 34 35 //5.处理数据 36 $row = $result->fetch_row(); 37 38 } 39 40 ?> 41 42 <form action="Update.php" method="post"> 43 44 <div><span style="width:80px;">代号:</span><input type="text" name="code" value="<?php echo $row[0] ?>" readonly="readonly" /></div> 45 46 <div><span style="width:80px;">姓名:</span><input type="text" name="name" value="<?php echo $row[1] ?>" /></div> 47 48 <div> 49 <span style="width:80px;">性别:</span> 50 <input type="radio" name="sex" value="true" <?php echo (bool)$row[2]?"checked=‘checked‘":"" ?> />男 51 <input type="radio" name="sex" value="false" <?php echo !(bool)$row[2]?"checked=‘checked‘":"" ?>/>女 52 </div> 53 54 <div><span style="width:80px;">民族:</span> 55 <select name="nation"> 56 <?php 57 //1.造连接对象 58 $db = new mysqli("localhost","root","123","mydb"); 59 60 //2.判断是否连接成功 61 if(mysqli_connect_error()) 62 { 63 echo "连接失败"; 64 65 //退出整个程序 66 exit; 67 } 68 else 69 { 70 //3.写SQL语句 71 $sql = "select * from Nation"; 72 73 //4.执行SQL语句 74 $result = $db->query($sql); 75 76 //5.处理数据 77 //遍历每一条数据 78 while($rownation = $result->fetch_row()) 79 { 80 if($rownation[0] == $row[3]) 81 { 82 echo "<option selected=‘selected‘ value=‘{$rownation[0]}‘>{$rownation[1]}</option>"; 83 } 84 else 85 { 86 echo "<option value=‘{$rownation[0]}‘>{$rownation[1]}</option>"; 87 } 88 89 } 90 } 91 ?> 92 </select> 93 </div> 94 95 <div><span style="width:80px;">生日:</span><input type="text" name="birthday" value="<?php echo $row[4] ?>" /></div> 96 97 <div><input type="submit" value="修改"/> <a href="Test.php">返回</a></div> 98 99 100 </form> 101 102 </body> 103 </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 = $_POST["code"]; 12 $name = $_POST["name"]; 13 $sex = $_POST["sex"]; 14 $nation = $_POST["nation"]; 15 $birthday = $_POST["birthday"]; 16 17 //1.造连接对象 18 $db = new mysqli("localhost","root","123","mydb"); 19 20 //2.判断是否连接成功 21 if(mysqli_connect_error()) 22 { 23 echo "连接失败"; 24 25 //退出整个程序 26 exit; 27 } 28 else 29 { 30 //3.写SQL语句 31 $sql = "update Info set Name=‘".$name."‘,Sex={$sex}, Nation=‘".$nation."‘,Birthday=‘".$birthday."‘ where Code = ‘".$code."‘"; 32 33 //4.执行SQL语句 34 $result = $db->query($sql); 35 36 //判断是否修改成功 37 38 if($result) 39 { 40 header("Location:Test.php"); 41 } 42 else 43 { 44 echo "修改失败"; 45 } 46 } 47 48 ?> 49 </body> 50 </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 13 //1.造连接对象 14 $db = new mysqli("localhost","root","123","mydb"); 15 16 //2.判断是否连接成功 17 if(mysqli_connect_error()) 18 { 19 echo "连接失败"; 20 21 //退出整个程序 22 exit; 23 } 24 else 25 { 26 //3.写SQL语句 27 $sql = "delete from Info where Code = ‘".$code."‘"; 28 29 //4.执行SQL语句 30 $result = $db->query($sql); 31 32 //判断是否删除成功 33 if($result) 34 { 35 header("Location:Test.php"); 36 } 37 else 38 { 39 echo "删除失败!"; 40 } 41 } 42 43 ?> 44 </body> 45 </html>
数据库访问
标签: