首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

php与数据库相关重要有关问题求解答

2012-05-23 
php与数据库相关重要问题求解答各位前辈高人,我在login.html中点击登录后直接转向userController.php,并且

php与数据库相关重要问题求解答
各位前辈高人,我在login.html中点击登录后直接转向userController.php,并且不显示任何东西,我的目的是验证登录成功后直接跳到mian.html 不成功则跳到error.html中,可是不成功,请问我哪里错了,还有一个问题就是登录成功后怎么吧登录者的信息传给mian.html?谢谢了


PHP code
 login.html<html><head>    <title>用户系统登录</title>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head><body><form name="loginfrom" action="userController.php?flag=login"method="POST"><table  background="resource/background.jpg" width= "100%"  height= "100%"   border= "0 "><tr><td align= "center "   valign= "middle ">    <table align="center">        <tr>            <th>角色</th>            <td><input type="radio" name="account"  value="admin" checked>管理员                <input type="radio" name="account" value="teacher">教师                <input type="radio" name="account"  value="student">学生            </td>        <tr>            <th>用户名</th>            <td><input type="text" name="user_name" ></td>        </tr>        <tr>            <th>密码</th>            <td><input type="password" name="user_password" ></td>        </tr>        <tr>            <td align="right" colspan="2"><a href="register.html">新用户注册</a><button type="submit" name="submit">登录</button></td>        </tr>    </table></td></tr></table></form></body></html>

PHP code
dbconn.class.php<?php    class DBconn{        private static $db_host="localhost";        private static $db_user="root";        private static $db_password="linux";        private static $db_name="sharewebDb";        private static $connection;        static function getConn(){            $connection=new mysqli($db_host,$db_user,password,$db_name);            return $connection;        }        }?>

PHP code
userModel.php<?php include("dbconn.class,php");class userModel{    function checklogin($userid,$userpwd,$useraccount){        $mysqli=DBconn::getConn();        $result='';        switch($useraccount){        case "teacher":{                $sql="select t_id,t_name,t_dep from ter_info where t_id=? and t_pwd=?";                $result=$mysqli->prepare($sql);:                $result->bind_param("ss",$user_id,$user_password);                $user_id=$userid;                $user_password=$userpwd;                $result->execute();break;                }        case "student":{                $sql="select s_id,s_name,s_dep from stu_info where s_id=? ands_pwd=?";                $result=$mysqli->prepare($sql);                $result->bind_param("ss",$user_id,$user_password);                $user_id=$userid;                $user_password=$userpwd;                $result->execute();break;                }        }        $result->bind_result($user_id,$user_name,$user_dep);        while($result->fetch()){            $userlist=array(                'user_id'=>$user_id,                'user_name'=>$user_name,                'user_dep'=>$user_dep            );                }        $result->close();        $mysqli->close();        return userlist;    }?> 


PHP code
userController.php<?phpinclude("userModel.php")session_start();if(isset($_REQUEST['flag'])){$flag=$_REQUEST['flag'];}switch($flag){case "login":    login();    break;}function login(){    $account=$_POST['account'];    $username=$_POST['user_name'];    $userpassword=$_POST['user_password'];    $usermodel=new userModel();    $login==usermodel->checklogin($username,$userpassword,$account);    if(isset($login)){        include("mian.html");        exit();    }else{header("Location:error.html")}}?>


[解决办法]
$connection=new mysqli($db_host,$db_user,password,$db_name);
password没有这个常量,你少了个 $ 。
更严重的是你要用 self::$db_host 这样的形式来访问类静态变量或方法才行
[解决办法]
用session记录登录状态。

另外也不用重定向来重定向去的,login.php就可以根据登录结果展示不同的内容,不需要再去写个文件叫做error.php,再写个main.php,把文件减少,写成不同的展现函数就行了。

热点排行