Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Can't register users

I cannot register users on my website. I am including the code from the relevant files below. I have not included any 3rd party code or libraries(monolog, composer, etc. into this program.) The code from the UserAuthentication course was referenced for this code.

This is the index.php file. <?php require DIR . 'sessions.php'; require DIR . 'connect.php'; ?>

<html> <head></head> <body> <label>log in</label>'; <form class="form-signin" method="post" action="login.php"> <label for = "email" name = "email"> email</label> <input type = "email" name = ""> <label for = "password" name = "password">password</label> <input type = "password" name = "password"> <button type = "submit"> Log In </button>

</form>'; <br> <br> label>register</label> <br> <br>

<form class="form-signin" method="post" action="register.php"> <label for = "first name">First Name</label> <input type = "text" name = "firstName"> <label for = "last name">Last Name</label> <input type = "text" name = "lastName"> <label for = "email">email</label> <input type = "email" name = "email"> <label for = "password">password</label> <input type = "password" name = "password"> <label for = "">confirm password</label> <input type = "password" name = "confirmPassword"> <label for = "date of birth">Date of Birth</label> <input type = "date" name = "dateOfBirth"> <label for = "city">City</label> <input type = "text" name = "city"> <label for = "state">State</label> <input type = "state" name = "state"> <label for = "zip">Zip Code</label> <input type = "text" name = "zipCode"> <label for = "gender">gender</label> <input type = "text" name = "gender"> <button type = "submit"> Register </button>

</form> </body> </html>

This is the session file.

<?php session_start(); ?>

This is the connect file.

<?php $servername = deleted for security reasons $username = deleted for security reasons $password = deleted for security reasons

try { $conn = new PDO ("mysql:host=$servername; dbname=userRegistrationSocial", $username, $password);

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected succesfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>

This is the registration file. <?php require DIR . 'functions.php';

$firstName = request()->get('firstName'); $lastName = request()->get('lastName'); $email = request()->get('email'); $password = request()->get('password'); $confirmPassword = request()->get('confirmPassword'); $dateOfBirth = request()->get('dateOfBirth'); $city = request()->get('city'); $state = request()->get('state'); $zipCode = request()->get('zipCode'); $gender = request()->get('gender');

if ($password != $confirmPassword) { redirect('/index.html'); }

$user = findUserByEmail($email); if (!empty($user)) { redirect('/register.php'); } $hashed = password_hash($password, PASSWORD_DEFAULT); // this is where the user first appears-> this variable $user is creatged from createUser function $user = createUser($firstName, $lastName, $email, $hashed, $dateOfBirth, $city, $state, $zipCode, $gender);

?>

This is the functions file. <?php

function findUserByEmail($email) { global $conn;

try {
    $query = "SELECT * FROM users WHERE email = :email";
    $stmt = $conn->prepare($query);
    $stmt->bindParam(':email', $email);
    $stmt->execute();
    return $stmt->fetch(PDO::FETCH_ASSOC);

} catch (\Exception $e) {
    throw $e;
}

}

function createUser($firstName, $lastName, $email, $hashed, $dateOfBirth, $city, $state, $zipCode, $gender) { global $conn;

try {
    $query = "INSERT INTO UserRegistration ('First Name', 'Last Name', 'email', 'password', 'date of birth', 'zip code', 'city', 'state', 'gender') VALUES ($firstName, $lastName, $email, $hashed, $dateOfBirth, $city, $state, $zipCode, $gender)";
    $stmt = $conn->prepare($query);
    $stmt->bindParam(':First Name', $firstName);
    $stmt->bindParam(':Last Name', $lastName);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':password', $password);
    $stmt->bindParam(':date of birth', $dateOfBirth);
    $stmt->bindParam(':city', $city);
    $stmt->bindParam(':state', $state);
    $stmt->bindParam(':zip code', $zipCode);
    $stmt->bindParam(':gender', $gender);       
    $stmt->execute();
    return findUserByEmail($email);
} catch (\Exception $e) {
    throw $e;
}

} ?>

Apologies for the bad format. I tried to put all of the code one post. I'm not sure why the code was split up like it is.