sirnails / BloomQuote

Quote management app for a florist
2 stars 0 forks source link

Bug: Fatal Error on User Registration with Existing Username #37

Open sirnails opened 5 months ago

sirnails commented 5 months ago

Describe the bug When attempting to register a new user with a username that already exists in the database, the system throws a fatal error instead of handling the duplicate entry gracefully. This results in an uncaught mysqli_sql_exception and the application crashes, displaying the error message to the user.

To Reproduce

  1. Go to the registration page of the website.
  2. Fill in the registration form with the following details:
    • Username: TestUser (or any existing username in the database)
    • Password: anything
  3. Email: test@user.com
  4. Submit the registration form.

Expected behavior The system should check for the existence of the username before attempting to insert it into the database. If the username already exists, an appropriate error message should be displayed to the user, such as "Username already taken. Please choose a different username."

Actual Behavior: The system attempts to insert the new user into the database without checking for duplicates, leading to a fatal error. The following error message is displayed:

Screenshots image

`


( ! ) Fatal error: Uncaught mysqli_sql_exception: Duplicate entry 'TestUser' for key 'users.username' in C:\wamp64\www\app\Models\User.php on line 15 -- 1 | 0.0001 | 450024 | {main}( ) | ...\index.php:0 2 | 0.0305 | 599360 | App\Controllers\UserController->register( ) | ...\index.php:138 3 | 0.0331 | 599952 | App\Models\User->register( $username = 'TestUser', $password = 'B20{E\|nr9REt', $email = 'test@user.com' ) | ...\UserController.php:37 4 | 0.1210 | 609736 | execute( ) | ...\User.php:15

( ! ) Fatal error: Uncaught mysqli_sql_exception: Duplicate entry 'TestUser' for key 'users.username' in C:\wamp64\www\app\Models\User.php on line 15 ( ! ) mysqli_sql_exception: Duplicate entry 'TestUser' for key 'users.username' in C:\wamp64\www\app\Models\User.php on line 15 Call Stack

Time Memory Function Location

1 0.0001 450024 {main}( ) ...\index.php:0 2 0.0305 599360 App\Controllers\UserController->register( ) ...\index.php:138 3 0.0331 599952 App\Models\User->register( $username = 'TestUser', $password = 'B20{E|nr9REt', $email = 'test@user.com' ) ...\UserController.php:37 4 0.1210 609736 execute( ) ...\User.php:15`

Desktop (please complete the following information):

Smartphone (please complete the following information):

Possible Solutions: Implement a check before attempting to insert a new user to verify if the username already exists in the database. Catch the mysqli_sql_exception and handle it gracefully by providing a user-friendly error message. Add a unique constraint error handler in the registration logic to manage duplicate entries without crashing.

Additional Information: The error occurs in the register method of the User model. It would be beneficial to review the logic in this method to ensure proper handling of duplicate entries. Additionally, consider implementing comprehensive error handling throughout the application to prevent similar issues in other areas.