用户登陆练习
form表单:
<html> <head><title>用户登陆</title> <meta http-equiv="content-type" content="text/html;charset=gb2312"> </head> <body> <form action="log.php" method="post"> <table border="1" width="250px"> <tr> <td colspan="2" align="center">用户登陆</td> </tr> <tr> <td>用户名:</td> <td> <input type="text" name="name"/> </td> </tr> <tr> <td>密码:</td> <td><input type="password" name="pwd"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="登陆"/> </td> </tr> </table> </form> </body> </html>
用户登陆操作数据库文件:
<?php if($_POST['name']!=null&&$_POST['pwd']!=null) { $name=$_POST['name']; $pwd=$_POST['pwd']; $link=mysql_connect('localhost','root','123'); $select=mysql_select_db('madb',$link); mysql_query("set names gb2312"); $log=mysql_query("select*from admin where name='$name' and pwd='$pwd'",$link); $rows=mysql_num_rows($log);//获取select语句查到的结果集中行的数目 echo $rows>0 ? "<script>alert('登录成功');</script>" : "<script>alert('登录失败');window.location.href='logform.php';</script>";//三元运算符 }else{ echo "<script>alert('请输入用户名和密码');window.location.href='logform.php';</script>"; } ?>