0

I am currently working on a simple registration form for an application that I'm building. I have nearly completed my build but am currently struggling with a registration form which is crucial to my project. I keep getting a HTTP 500 error saying "This page is not working" when I click the submit button on my form... I have tried a lot of different things to fix it but no luck... Any help is appreciated!

Here is the code:

session_start();

require 'connect.php';

if(isset($_POST['register'])){


$firstname = !empty($_POST['username']) ? trim($_POST['firstname']) : null;
$lastname = !empty($_POST['password']) ? trim($_POST['lastname']) : null;
$username = !empty($_POST['username']) ? trim($_POST['username']) : null;
$pass = !empty($_POST['password']) ? trim($_POST['password']) : null;

//TO ADD: Error checking (username characters, password length, etc).

$sql = "SELECT COUNT(username) AS num FROM credentials WHERE username = :username";
$stmt = $pdo->prepare($sql);

$stmt->bindValue(':username', $username);

//Execute.
$stmt->execute();

//Fetch the row.
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if($row['num'] > 0){
    die('That username already exists!');
}

$sql = "INSERT INTO credentials (username, password) VALUES (:username, :password)";
$stmt = $pdo->prepare($sql);

$stmt->bindValue(':username', $username);
$stmt->bindValue(':password', $password);

$result = $stmt->execute();

if($result){

    echo 'Thank you for registering.';
}

}

And I was also curious as to how you could log the user in right after they successfully submit their registration form and display their username on the welcome page... I am fairly new to PHP so I am very curious about some things! Thanks again guys!

M97
  • 59
  • 8
  • Why was this marked as a duplicate? – M97 Dec 04 '17 at 08:25
  • Change `$pass` to `$password`. (Your POST keys are mixed up also on the assignment lines.) Error reporting would have helped you debug this (assuming you have a constraint on your password field such as not null). – Progrock Dec 04 '17 at 09:01
  • And when you go to actually store and verify passwords, use `password_hash` and `password_verify`. – Progrock Dec 04 '17 at 09:09
  • I got it fixed and it's functioning correctly, for the most part that is... any idea how I could pop up an error in the form if username exists? and then starting a logged in session after a successful registration form submission? – M97 Dec 04 '17 at 09:09
  • Yup. But there are dozens of tutorials on the Internet that will already help you. – Progrock Dec 04 '17 at 09:11
  • I've really struggled finding a solution the the second question I asked... – M97 Dec 04 '17 at 09:13
  • Do follow the advice on the given link to help you debug pdo problems. Session management is confusing at first, but a good starting point is the docs. – Progrock Dec 04 '17 at 09:14
  • Gotcha, I appreciate your help! – M97 Dec 04 '17 at 09:14

0 Answers0