0

There's so many similar question but none of them have helped me figure this out. This is my first time using OOP PHP. I was following a youtube tutorial and I swear this code was working fine 2 weeks ago. But now its not. And its not even throwing and errors.

I am signing up a user, then created an account class and calling the 'createAccout' method on that class, where I pass in the username and password (I have stripped it down for testing, there will be proper validation and sensitization later).

In the 'createAccount' method, I have a prepared PDO statement in a try block. When trying to sign up, the code goes through the try block, then executes whats in the finally block. But when checking the database, there is no new row. I have tried this code on 2 machines and they both have the same results.

Sign up form:

<?php

include '../model/database.class.php';
include '../model/account.class.php';


if(isset($_POST['submit'])) { 
    $loginid = filter_input(INPUT_POST, 'username');
    $pword = filter_input(INPUT_POST, 'password');

    $loginTest = new Account();
    $loginTest->createAccount($loginid, $pword);
}

?>

<!-- Sign up form -->
<h2>Sign Up</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label for="usernameInput">Username</label>
    <input id="usernameInput" type="text" name="username">

    <label for="passwordInput">Password</label>
    <input id="passwordInput" type="password" name="password">

    <label for="passwordMatchInput">Re-type Password</label>
    <input id="passwordMatchInput" type="password" name="passwordMatch">

    <button type="submit" name="submit">Sign Up</button>
</form>

account class:

<?php

class Account extends Database{
    public function getLoginDetails($loginid) {
        $sql = "SELECT loginid, pword FROM account WHERE loginid = ?";
        $stmt = $this->connect()->prepare($sql);
        $stmt->execute([$loginid]);

        $results = $stmt->fetch(); 
        return $results;
    }

    public function createAccount($loginid, $pword) {
        try {
            $sql = "INSERT INTO `account`(`loginid`, `pword`) VALUES (:loginid, :pword)";
            $stmt = $this->connect()->prepare($sql);
            $stmt->bindParam(':loginid', $loginid);
            $stmt->bindParam(':pword', $pword);
            $stmt->execute();
        }
        catch(PDOException $e){
            header('location: ?message=errorInQuery');
            print "Error in query: " . $e->getMessage();
            exit();
        }
        finally{
            header('location: welcome.php');
            exit();
        }
    }
}

database class (getting extended by the account class)

<?php

class Database {
    private $host = "localhost";
    private $user = "dev";
    private $pwd = "healthcare";
    private $dbName = "login_test";

    protected function connect() {
            $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbName;
            $pdo = new PDO($dsn, $this->user, $this->pwd);
            $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            return $pdo;
    }
}

I used to be using a controller class, but I took that out too because I was isolating the issue, which seems to be getting the data into the database. My loginid is VARCHAR(10) and my pword is VARCHAR(255).

ryandougc
  • 109
  • 6
  • You should add your CREATE table or test it manually and i hope the password is hashed before you enter it. – nbk Mar 24 '20 at 16:25
  • do you get the value of `print "Error in query: " . $e->getMessage();`? maybe you could replace it with an `error_log` call? I would also set an `error_log("p: $pword | u: $loginid")` after `$loginid = filter_input(INPUT_POST, 'username'); $pword = filter_input(INPUT_POST, 'password');` that way you can see if you get the data, – JoSSte Mar 24 '20 at 16:31
  • The guy who closed this solved it for me, I didn't have PDO setup with the right attributes so it wouldn't report errors from mysql. I had issues with column names. @nbk I do have hashing, but I removed it for debugging since I couldn't even post anything to the db – ryandougc Mar 24 '20 at 16:47

0 Answers0