PHP User Registration Form with MySQL Database
This post will show you how to create A User Registration Form using PHP & MySQL
Prior to that you need to complete the items listed Below
- Mamp or xampp with phpMyAdmin install into your Computer , check out here
- A Sample Database created Check out Here
- New User Created . Check out Here
- A New table created , check out Here
- Insert Data into the Table Here
Create users Table
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `username` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, `password` varchar(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
User Registration PHP Code
<?php // Include connection config file include ("C:/xampp/htdocs/dbconnection.php"); // Define variables and initialize with empty values $username = $password = $confirm_password = ""; $username_err = $password_err = $confirm_password_err = ""; // Processing form data when form is submitted // Which request method was used to access the page; e.g. 'GET', 'HEAD', 'POST', 'PUT'. if($_SERVER["REQUEST_METHOD"] == "POST"){ //------------- Validate the User Name------------------------ // Trim to remove white Space // If the User did not enter anythng , prompt the error string " $username_err " embed in HTML text box below if(empty(trim($_POST["username"]))){ $username_err = "Please enter a username."; } // If User Name is Enter else{ // Prepare a select statement $sql = "SELECT id FROM users WHERE username = ?"; //(1) Prepare the statement : Return Bool if( $stmt = $dbConnectionStatus->prepare($sql)){ //(2) Bind variables to the prepared statement as parameters $stmt->bind_param('s',$param_username ); // (3) Get parameters settings $param_username = trim($_POST["username"]); // (4)Attempt to execute the prepared statement // If execution is true if($stmt->execute()){ /* store result */ $stmt->store_result(); //Check whether the User Name Existed in the Database if($stmt->num_rows == 1){ $username_err = "This username is already taken."; } else{ // If Username not exist Save the User Name $username = trim($_POST["username"]); } } else{ echo "There is an Error!."; } // Close statement mysqli_stmt_close($stmt); } } //------------------------------------------------------------- //------------- Validate Password------------------------------ // Check Whether the User Enter Pasword if(empty(trim($_POST["password"]))){ $password_err = "Please enter a password."; } else{ $password = trim($_POST["password"]); } //------------------------------------------------------------- //------------- Validate Password------------------------------ // Check Whether the User Enter Confirm Pasword if(empty(trim($_POST["confirm_password"]))){ $confirm_password_err = "Please confirm password."; } else{ $confirm_password = trim($_POST["confirm_password"]); // Check whether Password Match if(empty($password_err) && ($password != $confirm_password)){ $confirm_password_err = "Password did not match."; } } //------------------------------------------------------------- //------------- Insert New User Registered Data---------------- // Check Whether there are Error for all Input Box if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){ // (1)Prepare an insert statement $sql = "INSERT INTO users (username, password) VALUES (?, ?)"; if($stmt = $dbConnectionStatus->prepare($sql)){ // (2) Bind variables to the prepared statement as parameters $stmt->bind_param('ss',$param_username, $param_password ); // (3) Set parameters $param_username = $username; $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ // When Successfulyt Register it will Redirect to login page header("location: login.php"); } else{ echo "Something Wrong."; } // Close statement mysqli_stmt_close($stmt); } } //------------------------------------------------------------- } ?> <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css"> <style type="text/css"> body{ font: 14px sans-serif; } .wrapper{ width: 350px; padding: 20px; } </style> </head> <body> <div class="wrapper"> <h2>Sign Up</h2> <p>Fill in To create an account.</p> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <!--If $username_err not empty assign "" otherwise assign 'has_error'--> <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>"> <label>Username</label> <input type="text" name="username" class="form-control" value="<?php echo $username; ?>"> <span class="help-block"><?php echo $username_err; ?></span> </div> <!--If $password_err not empty assign "" otherwise assign 'has_error'--> <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>"> <label>Password</label> <input type="password" name="password" class="form-control" value="<?php echo $password; ?>"> <span class="help-block"><?php echo $password_err; ?></span> </div> <!--If $confirm_password_err not empty assign "" otherwise assign 'has_error'--> <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>"> <label>Confirm Password</label> <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>"> <span class="help-block"><?php echo $confirm_password_err; ?></span> </div> <div class="form-group"> <input type="submit" class="btn btn-primary" value="Submit"> <input type="reset" class="btn btn-default" value="Reset"> </div> <p>Already have an account? <a href="login.php">Login here</a>.</p> </form> </div> </body> </html>