登录&注册用户// PHP / MYSQL OOP

时间:2013-11-24 09:57:07

标签: php mysql oop login passwords

这会让我发疯... 那么,我做错了什么?我的错误在哪里?

当我尝试连接时,它总是显示用户和密码错误的结果,即使它们是正确的......

如果有人有想法实施会话?

提前感谢您的帮助

和平& amp;团结!

这是野兽!

的index.php

<?php 

include_once('user.php');

if(isset($_POST[submit])){
    $name = $_POST['login'];
    $pass = $_POST['password'];


    $object = new User() ;
    $object->Login($name, $pass); 

    }

?>


<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
</head>

<body>
    <form method="post" action="index.php">
    login : <input type="text" name="login" /><br>
    password : <input type="text" name="password" />
    <input type="submit" name="submit"  value="login" />

    </form>



</body>
</html>

dbConnect.php

<?php 

class Connection {

    public function dbConnect(){

        return new PDO("mysql:host=localhost; dbname:test","root","");

        }


    }


?>

user.php的

<?php 

include_once('dbConnect.php');

Class User {
    //Start Construct
    Private $db;

    public function __construct () {
            $this->db = new Connection();
            $this->db = $this->db->dbConnect();

        } 
// End Construct

public function Login($name, $pass) {

    if(!empty($name) && !empty($pass)){
        $st = $this->db->prepare("SELECT * FROM user Where user_login=? AND user_pass=?");
        $st->bindParam(1, $name);
        $st->bindParam(2, $pass);
        $st->execute();

        if($st->rowCount() == 1){
            echo "User Connected";
            }else{

                echo "Vachez ta mère";
                }


        }else{

            echo "Enter a valid Login and Password";
            }





    }


}
?>

2 个答案:

答案 0 :(得分:0)

使用$st = $this->db->prepare("SELECT * FROM user Where user_login=? AND user_pass=?"); $st->execute(array($name, $pass));

而不是  $st = $this->db->prepare("SELECT * FROM user Where user_login=? AND user_pass=?"); $st->bindParam(1, $name); $st->bindParam(2, $pass); $st->execute();

答案 1 :(得分:-3)

您可以使用这样的传统方法:

公共函数登录($ name,$ pass){

if(!empty($name) && !empty($pass)){
    $st = mysql_query("SELECT * FROM user Where user_login=? AND user_pass=?");
    $count = mysql_fetch_rows($st);
    if($count == 1){
        echo "User Connected";
        $userSession = $_SESSION['user'];
        }else{

            echo "Vachez ta mère";
            }


    }else{

        echo "Enter a valid Login and Password";
        //header("location: index.php"); <- this can be used to redirect
        }





}