php数据访问(二)(注册,登录,删除)
info表
nation表
一.注册
1.注册页面
1 <body> 2 <h1>注册</h1> 3 <form action="chuli.php" method="post"> 4 <div>用户名:<input type="text" name="uid" /></div> 5 <div>密码:<input type="password" name="pwd" /></div> 6 <div>姓名:<input type="text" name="name" /></div> 7 <div>性别: 8 男<input type="radio" name="sex" value="1" /> 9 女<input type="radio" name="sex" value="0" /> 10 </div> 11 <div>生日:<input type="text" name="birthday" /></div> 12 <div> 13 民族: 14 <select name="nation"> 15 <?php 16 $db = new MySQLi("localhost","root","123","test_123");// 17 $sqln = "select * from nation"; 18 $resultn = $db->query($sqln); 19 $arrn = $resultn->fetch_all(); 20 foreach($arrn as $v) 21 { 22 echo "<option value='{$v[0]}'>{$v[1]}</option>"; 23 } 24 ?> 25 </select> 26 </div> 27 <div><input type="submit" value="注册" /></div> 28 </form> 29 </body>
2.注册处理页面(向数据库的表中写入数据)chuli.php
1 <?php 2 header("Content-type:text/html;charset=utf-8");//设置页面内容是html编码格式是utf-8 3 //1.取提交的数据 4 $uid = $_POST["uid"]; 5 $pwd = $_POST["pwd"]; 6 $name = $_POST["name"]; 7 $sex = $_POST["sex"]; 8 $birthday = $_POST["birthday"]; 9 $nation = $_POST["nation"]; 10 11 //2.向数据库写入 12 $db = new MySQLi("localhost","root","123","test_123"); 13 $sql = "insert into info values('{$uid}','{$pwd}','','{$name}',{$sex},'{$birthday}','{$nation}')"; 14 15 if($db->query($sql)) 16 { 17 echo "注册成功"; 18 } 19 else 20 { 21 echo "注册失败"; 22 }
二.登录
1.登录页面
1 <body> 2 <h1>登录页面</h1> 3 <form action="denglu.php" method="post"> 4 <div>用户名:<input type="text" name="uid" /></div> 5 <div>密码:<input type="password" name="pwd"/></div> 6 <div><input type="submit" value="登录" /></div> 7 </form> 8 </body>
2.登录处理页面 denglu.php
1 <?php 2 $uid = $_POST["uid"]; 3 $pwd = $_POST["pwd"]; 4 $db = new MySQLi("localhost","root","123","test_123"); 5 /*$sql = "select count(*) from info where uid='{$uid}' and pwd='{$pwd}'"; 6 $result = $db->query($sql); 7 $arr = $result->fetch_row(); 8 if($arr[0]) 9 { 10 echo "登录成功"; 11 } 12 else 13 { 14 echo "登录失败"; 15 }*/ 16 //此方法容易受到 a' or 1=1 #(SQL注入攻击) 17 18 $sql = "select pwd from info where uid='{$uid}'"; 19 $result = $db->query($sql); 20 $arr = $result->fetch_row(); 21 if($arr[0]==$pwd && !empty($pwd)) 22 { 23 echo "登录成功"; 24 } 25 else 26 { 27 echo "登录失败"; 28 }
三.删除
1.显示info表信息页面
1 <body> 2 <h1>显示info信息</h1> 3 <table width="100%" border="1" cellpadding="0" cellspacing="0"> 4 <tr> 5 <td>代号</td> 6 <td>姓名</td> 7 <td>性别</td> 8 <td>生日</td> 9 <td>民族</td> 10 <td>操作</td> 11 </tr> 12 13 <?php 14 $db = new MySQLi("localhost","root","123","test_123"); 15 $sql = "select * from info"; 16 $result = $db->query($sql); 17 $arr = $result->fetch_all(); 18 foreach($arr as $v) 19 { 20 //修改性别 21 $sex = $v[4]?"男":"女"; 22 23 //修改民族 24 $sql1 = "select nno from nation where code='{$v[6]}'"; 25 $r1 = $db->query($sql1); 26 $a1 = $r1->fetch_row(); 27 28 echo "<tr> 29 <td>{$v[2]}</td> 30 <td>{$v[3]}</td> 31 <td>{$sex}</td> 32 <td>{$v[5]}</td> 33 <td>{$a1[0]}</td> 34 <td><a href='del.php?code={$v[2]}' onclick=\"return confirm('确认删除么?')\">删除</a> 35 <a href='update.php?code={$v[2]}'>修改</a> 36 </td> 37 </tr>"; 38 } 39 ?> 40 41 </table> 42 </body> 43 <script type="text/javascript"> 44 //confirm("确定删除么"); 45 </script>
2.删除处理页面
1 <?php 2 $code=$_GET["code"]; 3 $db = new MySQLi("localhost","root","123","test_123"); 4 $sql = "delete from info where code='{$code}'"; 5 if($db->query($sql)) 6 { 7 //跳转页面方法1 8 header("location:main.php"); //注意location不要写错 9 //跳转页面方法2 10 /*echo "<script>window.location.href='main.php'</script>";*/ 11 } 12 else 13 { 14 echo "删除失败!"; 15 }