第一个页面login.php
<h1>登陆页面</h1> <form action="chuli.php" method="post"> <div>用户名:<input type="text" name="uid"/></div> <div>用户名:<input type="password" name="pwd"/></div> <div><input type="submit" value="登陆" /></div> </form>
第二个页面chuli.php
<?php $uid=$_POST["uid"]; $pwd=$_POST["pwd"]; $db=new MySQLi("localhost","root","root","dbname"); //$sql="select count(*) from users where uid='$uid' and pwd='[$pwd]'";能注入攻击,不采用 $sql="select pwd from users where uid='{$uid}'";//花括号是PHP中解析语句用的,‘’是SQL语句中字符串用的 $result=$db->query($sql); $a=$result->fetch_row(); if(!empty($pwd)&&$a[0]==$pwd){ //跳转页面 //header("location:main.php"); //(1) echo"<script>window.location = 'main.php'</script>"; } //(2)两种皆可 else { echo"用户名或密码错误"; } ?>
1、注意$sql语句和判断语句的写法
2、两种跳转页面的方式
第三个页面main.php<table border="1" width="100%" cellpadding="0" cellspacing="0">
<tr> <td>号码</td> <td>姓名</td> <td>性别</td> <td>种族</td> <td>生日</td> <td>操作</td> </tr> <?php $db=new MySQLi("localhost","root","root","dbname"); $sql="select * from info"; $result =$db->query($sql); if($result) { while($attr=$result->fetch_row()) { //对性别进行处理 $sex=$attr[2]?"男":"女"; //对民族进行处理 $sname="select name from nation where code='{$attr[3]}'"; $nresult=$db->query($sname); $aname=$nresult->fetch_row(); $nation=$aname[0]; echo"<tr> <td>{$attr[0]}</td> <td>{$attr[1]}</td> <td>{$sex}</td> <td>{$nation}</td> <td>{$attr[4]}</td> <td><a onclick="return confirm('确定要删除吗?')" href='shanchu.php?code={$attr[0]}'>删除</a>
<a href='xiugai.php?code={$attr[0]}'>修改</a>
</td> </tr>"; } } ?> </table>
<a href="tianjia.php">添加数据</a>
1、性别处理时的写法
2、民族处理时从另外一个表格中再查内容
3、转义字符的应用(本篇onclick语句中单双引号的冲突)
第四个页面shanchu.php
<?php $code=$_GET["code"]; $db=new MySQLi("localhost","root","root","dbname"); $sql="delete from info where code ='{$code}'"; if($db->query($sql)) { header("location:main.php"); } else { echo"删除失败"; } ?>