0

It's going to drive me crazy ... So, What am i doing wrong ? Where is my error ?

When i try to connect, it always show me the result where user and password are wrong even they're right ...

If someone has an idea to implement session also ?

Thanks in advance for your help

Peace & Unity !

Here's the beast !

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";
            }





    }


}
?>
user3026969
  • 1
  • 1
  • 2
  • Have you enabled full error reporting? Also, you should set PDO to use exception for error handling and disable emulated prepares. [This tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers) might help a bit. Oh, and the PDO instance would better passed as a dependency to `User` class constructor. To share the same PDO instance among multiple classes, you could employ [this approach](http://stackoverflow.com/a/11369679/727208). – tereško Nov 24 '13 at 10:02
  • Thanks for your feedback, i will check that ! – user3026969 Nov 24 '13 at 10:07
  • The first link is exactly what i'have used to do my page. Could you more specific in _To share the same PDO instance among multiple classes, you could employ this approach_ – user3026969 Nov 24 '13 at 10:33
  • Which part should I specify? It contained a link already. – tereško Nov 24 '13 at 10:41
  • Yes absolutely, but it's chinese for me, ;-) – user3026969 Nov 24 '13 at 10:46
  • to be more specific, it took me maybe 8 hours to code these 3 pages. – user3026969 Nov 24 '13 at 10:48
  • Then there is nothing I can do. You will probably be better off if you start reading [this book](http://www.wrox.com/WileyCDA/WroxTitle/Beginning-PHP-5-3.productCd-0470413964.html). – tereško Nov 24 '13 at 10:53
  • I have already read this book [link](http://www.amazon.com/Expert-PHP-MySQL-Application-Development-ebook/dp/B00FJUFEKW/ref=sr_1_15?s=books&ie=UTF8&qid=1385297344&sr=1-15&keywords=php) and all i have done was basically based on this book and web research. So now, i'm stuck and you seem to be very good, why don't you tell what's going on this code ? thanks again for your very precious help – user3026969 Nov 24 '13 at 12:50
  • Try to change this: `if($st->rowCount() == 1){` to this: `if($st->rowCount() > 0){` just in case, if you allow duplicate usernames. Also note this: PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object. Check if you connect to the DB and if the user exists in your db. – Jo Smo Aug 03 '14 at 10:42

2 Answers2

0

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

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

Lakmal Vithanage
  • 2,767
  • 7
  • 42
  • 58
-3

You can use the tradational method like this :

public function Login($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
        }





}