PDO OOP Logowanie - początki

0

Witam, piszę apkę webową i zaczynam od Panelu Logowania. Napisałem kod klasy łączącej się z bazą danych, wstępne logowanie bez weryfikacji danych, oraz formularzyk w HTMLu. Mam jakiś niewielki problem z zalogowaniem się i nie mogę się go doszukać. Jak klikam login to wyświetla mi: Incorrect username or password. Proszę o pomoc.

User.php:

<?php

/**
 * Created by PhpStorm.
 * User: ziben
 * Date: 18.10.2015
 * Time: 17:41
 */
include_once('ConnectionDB.php');
class User
{
    private $db;

    public function __construct(){
        $this->db = new ConnectionDB();
        $this->db = $this->db->getConnection();
    }

public function Login($name,$pass){
    if(!empty($name) && !empty($pass)){
        $st = $this->db->prepare("select * from users where name=? and pass=?");
        $st->bindParam(1, $name);
        $st->bindParam(2, $pass);
        $st->execute();

        if($st->rowCount() > 0){
            echo "User verified, Access granted.";
        } else {
            echo "Incorrect username or password";
        }

    }else{
        echo "Please enter username and password";
    }
}
}
 

index.php

<?php
/**
 * Created by PhpStorm.
 * User: ziben
 * Date: 18.10.2015
 * Time: 14:42
 */

include_once('User.php');
if(isset($_POST['submit'])){
    $name = $_POST['user'];
    $pass = $_POST['pass'];

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

<!DOCTYPE html>
    <html>
<head>
    <meta charset="UTF-8">
    <title>Panel logowania</title>
</head>
<body>
<form method="post" action="index.php">
    Login: <input type="text" name="user"/>
    Password: <input type="password" name="pass"/>
    <input type="submit" name="submit" value="Login"/>
</form>
</body>
</html>
 

ConnectionDB.php <- tu jest raczej dobrze, jedyny problem jest taki, że jak odświeżę index.php to tak jakby odświeża, film przedstawia sytuację: http://speedy.sh/HkgSA/2015-10-18-at-19-52-21.mp4

 
<?php
/**
 * Created by PhpStorm.
 * User: ziben
 * Date: 17.10.2015
 * Time: 20:31
 */

class ConnectionDB{
    //Specyfikacja bazy danych
    private $host = "localhost";
    private $base = "mojabaza";
    private $username = "admin";
    private $password = "admin";
    public $conn;

    //Połączenie z bazą danych
    public function getConnection(){
        $this->conn = null;
        try{
            $this->conn = new PDO("mysql:host=" .$this->host . ";base=" . $this->base, $this->username, $this->password);
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }
        return $this->conn;
    }
}
?>

i zdjęcie z phpMyAdmin:
user image

Z góry dzięki za wszelką pomoc.

0

Zamień znaki zapytania na zmienne.

1
$data = $st->fetchAll();

I sprawdzasz czy coś masz w tablicy $data. Sztuczka z użyciem rowCount w kontekście zapytania select owszem działa, ale dokumentacja mówi, że:

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

Inna opcja:
http://php.net/manual/en/pdostatement.fetchcolumn.php

1

rowCount() nie stosuje się do SELECTA:
http://php.net/manual/en/pdostatement.rowcount.php
Zamiast tego użyj fetch()
http://php.net/manual/en/pdostatement.fetch.php

0

Już testuję :P kilka minutek :)

0
 if(!empty($name) && !empty($pass)){
        $st = $this->db->prepare("select * from users where name='$name' and pass='$pass'");
        $st->execute();
        $result = $st->fetchColumn();

        if($result > 0){
            echo "User verified, Access granted.";
        } else {
            echo "Incorrect username or password";
        }

    }else{
        echo "Please enter username and password";
    }

zmieniłem i dalej Incorrect username or password wyskakuje

0
 if(!empty($name) && !empty($pass)){
        $st = $this->db->prepare("select * from users where name=? and pass=?");
        $st->execute(array($name, $pass));
        $data = $st->fetchAll();
 
        if(count($data) > 0){
            echo "User verified, Access granted.";
        } else {
            echo "Incorrect username or password";
        }
 
    }else{
        echo "Please enter username and password";
    }

Jak znów będą problemy to var_dump($data) i pokaż co zwróciło.

0

array(0) { } Incorrect username or password

0

No nie mam pojęcia co jest nie tak, ślęczę nad tym i nie widzę. Siądę jeszcze jutro z rana i zrobię całkiem nową bazę, zobaczymy.

0

Zrobiłem bazę od nowa, jednak dalej to samo. Co może być przyczyną ?

0
 $st = $this->db->prepare("INSERT INTO users(name, pass) VALUES('Jan','Nowak')");

nawet INSERT nie działa, help me.

0

Dobra poddaję się, piszę do promotora o zmianę języka na javę. Szkoda się tak męczyć. Dzień kolejny leci, a ja stoję w tym samym punkcie. Kombinuję na wszystkie strony i z bazą i z tymi plikami powyżej i cały czas pusty array, nie widzi wierszy w tabeli i tyle. Tragedia i parodia zarazem.

0

Jeszcze coś takiego znalazłem w php error log

[19-Oct-2015 0919 UTC] PHP Warning: PHP Startup: PDO: Unable to initialize module
Module compiled with module API=20050922
PHP compiled with module API=20131226
These options need to match
in Unknown on line 0

może to wina tego ?

1

Pomyliłem się z tymi znakami ?. Zostaw je. Już wiem, gdzie masz błąd. Mianowicie jest on w pliku ConnectionDB.php. Masz błąd w linijce 21. Zmień z

$this->conn = new PDO("mysql:host=" .$this->host . ";base=" . $this->base, $this->username, $this->password);

na

$this->conn = new PDO("mysql:host=" .$this->host . ";dbname=" . $this->base, $this->username, $this->password);

.

0

Projekt dawno zamknięty :) teraz z asp.net morduję hehe zobacz moje tematy

1 użytkowników online, w tym zalogowanych: 0, gości: 1