Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:44:42+00:00 2026-06-11T18:44:42+00:00

I have a register.php file which takes the input from a form and tries

  • 0

I have a register.php file which takes the input from a form and tries to create a user account in a database. I have an innoDB MySQL database which has a ‘UNIQUE’ key added to the ‘username’ field of the users table. This is to prevent duplicate usernames.

Below is an extract from my php code. In my database I currently have a user called ‘testUser’. However, when running the code below with $username = testUser, the php file returns ‘Account created’, even though a duplicate row is not created in the database. I would have expected there to be an exception thrown if the user already existed?

try{
    $stmt = $db->prepare("INSERT INTO users (username, password, salt) VALUES (:username,   :password, :salt)");
    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':password', $hash);
    $stmt->bindParam(':salt', $salt);
    $stmt->execute();
}catch(PDOException $e){
    returnJSON(0,'Error creating account');
}

returnJSON(1,'Account created');

function returnJSON($errorCode, $message){
    $arr = array("returnCode" => $errorCode, "returnMessage" => $message);
    echo json_encode($arr);
    die();
}

 ----
try{
    $db = new PDO('mysql:host='.$host.';dbname='.$dbname, $username, $password);
}
catch(PDOException $e){
    $arr = array("returnCode" => 0, "returnMessage" => "unable to connect to server");
    echo json_encode($arr);
    die();
}

My question is, how should I find out if the user’s account was successfully added to the database.

PS. I do have code to query the database first and see if there exists a user with the same name before calling this code. But I’d still like to fix/understand this as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:44:43+00:00Added an answer on June 11, 2026 at 6:44 pm

    PDO by default has silent errors: you need to check then yourself by actually looking for them.

    If you want to use exceptions, you need to tell PDO to do it. Check http://php.net/manual/en/pdo.error-handling.php

    The specific code you want is

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    in the try/catch immediately after you create the connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Create new account page. The form is in page named Register.php
i have made user-register.tpl.php file. And i have set many text field in that.
I have a register.php file which creates new users for my site . But
I currently have a PHP file which will search my MySQL database and see
I am trying to create simple user registration form. I have an index.html file
On my website I have a PHP script which (when user enters a username
I have just finished creating an entire login and register systsem in PHP, but
I have php scripts that do things like register, login, upload. I would like
I have a register page that saves the data to a database the only
I have a registration form that user submits, data is sent using isset($_POST) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.