php数据分析教程学习 php数据类型分为哪几类( 九 )


下面综合一下前面所说的我们来看一个登陆程序 , 判断用户名密码是否正确 。
登陆表单是这样:login.php
table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0"
tr
form action="checklogin.php" method="post"td align="center" valign="middle"table width="400" border="0" cellpadding="5" cellspacing="1" class="tablebg"
tr class="tdbg"
td colspan="2"p align="center"Administrators Login/p/td
/tr
tr class="tdbg"
tdp align="center"Username/p/td
tdp align="center"
input name="username" type="text" id="username"
/p/td
/tr
tr class="tdbg"
tdp align="center"Password/p/td
tdp align="center"
input name="password" type="password" id="password"
/p/td
/tr
tr class="tdbg"
td colspan="2"p align="center"
input type="submit" name="Submit" value="https://www.04ip.com/post/Submit"
input type="reset" name="Submit2" value="https://www.04ip.com/post/Clear"
/p/td
/tr
/table/td/form
/tr
/table
处理文件是这样
?php
require_once('conn.php');
session_start();
$username=$_POST['username'];
$password=$_POST['password'];
$exec="select * from admin where username='".$username."'";
if($result=mysql_query($exec))
{
if($rs=mysql_fetch_object($result))
{
if($rs-password==$password)
{
$_SESSION['adminname']=$username;
header("location:index.php");
}
else
{
echo "scriptalert('Password Check Error!');location.href='https://www.04ip.com/post/login.php';/script";
}
}
else
{
echo "scriptalert('Username Check Error!');location.href='https://www.04ip.com/post/login.php';/script";
}
}
else
{
echo "scriptalert('Database Connection Error!');location.href='https://www.04ip.com/post/login.php';/script";
}
?
conn.php是这样:
?php
$conn=mysql_connect ("127.0.0.1", "", "");
mysql_select_db("shop");
?
由于 $_SESSION['adminname']=$username;我们可以这样写验证是否登陆语句的文件:checkadmin.php
?php
session_start();
if($_SESSION['adminname']=='')
{
echo "scriptalert('Please Login First');location.href='https://www.04ip.com/post/login.php';/script";
}
?
做一个分页显示
关键就是用到了SQL语句中的limit来限定显示的记录从几到几 。我们需要一个记录当前页的变量$page , 还需要总共的记录数$num
对于$page如果没有我们就让它=0,如果有0就让它也=0,如果超过了总的页数就让他=总的页数 。
$execc="select count(*) from tablename ";
$resultc=mysql_query($execc);
$rsc=mysql_fetch_array($resultc);
$num=$rsc[0];
这样可以得到记录总数
ceil($num/10))如果一页10记录的话 , 这个就是总的页数
所以可以这么写
if(empty($_GET['page']))
{
$page=0;
}
else
{
$page=$_GET['page'];
if($page0)$page=0;
if($page=ceil($num/10))$page=ceil($num/10)-1;//因为page是从0开始的,所以要-1
}
这样$exec可以这么写 $exec="select * from tablename limit ".($page*10).",10";
//一页是10记录的
最后我们需要做的就是几个连接:
a href="https://www.04ip.com/post/xxx.php?page=0"FirstPage/a
a href="https://www.04ip.com/post/xxx.php?page=?=($page-1)?"PrevPage/a
a href="https://www.04ip.com/post/xxx.php?page=?=($page+1)?"NextPage/a
a href="https://www.04ip.com/post/xxx.php?page=?=ceil($num/10)-1?"LastPage/a
注意事项
1、注意不要漏了分号
2、注意不要漏了变量前的$
3、使用SESSION的时候注意不要遗漏session_start();
如果发生错误的时候 , 可以采用以下方法:

推荐阅读