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 trialStu Cowley
5,801 PointsUncaught PDOException Error
Hi everyone!
I keep getting a Uncaught PDOException: SQLSTATE[23000] error from my createUser
function. I have followed along with the video exact, I even downloaded the and copy and pasted the createUser
function.
Here is the function:
function createUser($email, $password) {
global $db;
try {
$query = "INSERT INTO users (email, password, role_id) VALUES (:email, :password, 2)";
$stmt = $db->prepare($query);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':password', $password);
$stmt->execute();
return findUserByEmail($email);
} catch (\Exception $e) {
throw $e;
}
}
Here is the error I keep getting:
Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: users.password in /Users/stucowley/Dropbox/Projects/Training/php-user-authentication/inc/functions.php:199 Stack trace: #0 /Users/stucowley/Dropbox/Projects/Training/php-user-authentication/inc/functions.php(199): PDOStatement->execute() #1 /Users/stucowley/Dropbox/Projects/Training/php-user-authentication/procedures/doRegister.php(22): createUser('stucowley@outlo...', NULL) #2 {main} thrown in /Users/stucowley/Dropbox/Projects/Training/php-user-authentication/inc/functions.php on line 199
I would appreciate some help with this as I am a bit confused as to why it isn't working.
Thanking you all in advance
Stu 😊
1 Answer
Chris Shaw
26,676 PointsHi Stu,
Have you checked that the $password
value isn't empty? The error is suggesting your column has a constraint that prevent empty or null values from being entered.
Stu Cowley
5,801 PointsStu Cowley
5,801 PointsBoom! That did it, thanks Chris Shaw 👍