Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // function to insert data in the database
- function registerusers($db) {
- if(!isset($_POST['submit'])) {
- return; }
- $errMsg = '';
- // get the data from the front end
- $username = $_POST['username'];
- $email = $_POST['email'];
- $password = $_POST['password'];
- $passwordconf = $_POST['password_confirm'];
- // check the values
- if( !$username || !$email || !$password || !$passwordconf) {
- echo 'One or more fields are empty.';
- return;
- }
- else {
- // escape special characters in a string for use in the SQL statement
- }
- // ENCRYPT THE PASSWORD
- $encrypt_pass = password_hash($password, PASSWORD_DEFAULT);
- if(($_POST["password"])!=($_POST["password_confirm"])){
- echo '<div id="alert1" class="alert alert-danger alert-dismissible" role="alert">
- <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>Oops! Password did not match! Try again..</div>';
- return;
- }
- // create a query
- $sqlQuery = "INSERT INTO register (username, email, password) VALUES (:username, :email, :password)";
- //prepare the query
- $query = $db->prepare($sqlQuery);
- //execute the query
- $query->execute(array(
- ':username' => $username,
- ':email' => $email,
- ':password' => $encrypt_pass
- ));
- // check if the student was successfully inserted in the database
- if ($query) {
- echo '<div id="alert1" class="alert alert-success alert-dismissible" role="alert">
- <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>You have successfully registered. You can now login <a href="login.php">login</a></div>';
- }
- else {
- // print the error generated
- echo '<div id="alert1" class="alert alert-danger alert-dismissible" role="alert">
- <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>Oops! Something went wrong here. Your registration was not successful, please try again.</div>';
- }
- }
- /* Main body */
- //connect to the DB and call the function
- require 'component/connect.php';
- registerusers($db);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement