0

This is the register.php it links to the User.class.php ** **I am working with the PDO statement for the database connection **When I click on the submit button the form is send to my database but it doesn't open the index.php **

include_once("classes/User.class.php");

try {
    if( !empty ($_POST)){
        if($_POST['password'] == $_POST['password_confirmation']) {
            $user = new User();
            $user->setEmail($_POST['email']);
            $user->setUsername($_POST['username']);
            $user->setFullname($_POST['fullname']);
            $user->setPassword($_POST['password']);
            if($user->register()){
                $user->login();
            }
        }
    }
}
catch (Exception $e) {
    $feedback = $e->getMessage();
}
?>

**This is the User.class.php **

   class User {
    private $email;
    private $username;
    private $fullname;
    private $password;

    public function register(){
        //connection
        $conn = new PDO('mysql:host='localhost'; dbname='databasename'', 'root', 'root');
        //query (insert)
        $statement = $conn->prepare("insert into users (email, username, fullname, password) 
        values(:email, :username, :fullname, :password)");


        // bcrypt
        $options = [ 'cost'=> 12 ];
        $password = password_hash($this->password, PASSWORD_DEFAULT, $options);


        $statement->bindParam(':fullname', $fullname);
        $statement->bindParam(':email', $email);
        $statement->bindParam(':username', $username);
        $statement->bindParam(':password', $password);
        //execute
        $result = $statement->execute();
        //return true/false
        return $result;
    }
    public function login() {
        if(!isset($_SESSION['loggedin'])) {
            header('Location:login.php');
            echo $feedback = "thanks for creating an account.";
    }


    }


}
Vos
  • 1
  • 1

1 Answers1

0

Your line 3 on User.class.php have an error it should be like this:

$conn = new PDO('mysql:host=HOSTNAME;port=3306;dbname=DATABASENAME;charset=UTF8;', USERNAME, PASSWORD);

change HOSTNAME, DATABASENAME, USERNAME and PASSWORD to the correct.

Roger Russel
  • 714
  • 7
  • 17