Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include config file
- error_reporting(-1);
- ini_set('display_errors', 'On');
- require_once "config.php";
- // Define variables and initialize with empty values
- $username = $name = $phone = $email = $password = $confirm_password = "";
- $username_err = $name_err = $phone_err = $email_err = $password_err = $confirm_password_err = "";
- // Processing form data when form is submitted
- if($_SERVER["REQUEST_METHOD"] == "POST"){
- // Validate username
- if(empty(trim($_POST["username"]))){
- $username_err = "Please enter a username.";
- } else{
- // Prepare a select statement
- $sql = "SELECT id FROM users WHERE username = ?";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "s", $param_username);
- // Set parameters
- $param_username = trim($_POST["username"]);
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- /* store result */
- mysqli_stmt_store_result($stmt);
- if(mysqli_stmt_num_rows($stmt) == 1){
- $username_err = "This username is already taken.";
- } else{
- $username = trim($_POST["username"]);
- }
- } else{
- echo "Oops! Something went wrong. Please try again lateru.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- ///////////////////////////////////////////////////////////////////////////
- // Validate name
- if(empty(trim($_POST["name"]))){
- $name_err = "Please enter a name.";
- } else{
- // Prepare a select statement
- $sql = "SELECT id FROM users WHERE name = ?";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "s", $param_name);
- // Set parameters
- $param_name = trim($_POST["name"]);
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- /* store result */
- mysqli_stmt_store_result($stmt);
- if(mysqli_stmt_num_rows($stmt) == 1){
- $name_err = "This name is already taken.";
- } else{
- $name = trim($_POST["name"]);
- }
- } else{
- echo "Oops! Something went wrong. Please try again latern.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Validate phone
- if(empty(trim($_POST["phone"]))){
- $phone_err = "Please enter a phone.";
- } else{
- // Prepare a select statement
- $sql = "SELECT id FROM users WHERE phone = ?";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "s", $param_phone);
- // Set parameters
- $param_phone = trim($_POST["phone"]);
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- /* store result */
- mysqli_stmt_store_result($stmt);
- if(mysqli_stmt_num_rows($stmt) == 1){
- $phone_err = "This phone is already taken.";
- } else{
- $phone = trim($_POST["phone"]);
- }
- } else{
- echo "Oops! Something went wrong. Please try again laterp.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Validate email
- if(empty(trim($_POST["email"]))){
- $email_err = "Please enter a name.";
- } else{
- // Prepare a select statement
- $sql = "SELECT id FROM users WHERE email = ?";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "s", $param_email);
- // Set parameters
- $param_email = trim($_POST["email"]);
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- /* store result */
- mysqli_stmt_store_result($stmt);
- if(mysqli_stmt_num_rows($stmt) == 1){
- $email_err = "This email is already taken.";
- } else{
- $email = trim($_POST["email"]);
- }
- } else{
- echo "Oops! Something went wrong. Please try again laterm.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Validate password
- if(empty(trim($_POST["password"]))){
- $password_err = "Please enter a password.";
- } elseif(strlen(trim($_POST["password"])) < 6){
- $password_err = "Password must have atleast 6 characters.";
- } else{
- $password = trim($_POST["password"]);
- }
- // Validate confirm password
- if(empty(trim($_POST["confirm_password"]))){
- $confirm_password_err = "Please confirm password.";
- } else{
- $confirm_password = trim($_POST["confirm_password"]);
- if(empty($password_err) && ($password != $confirm_password)){
- $confirm_password_err = "Password did not match.";
- }
- }
- // Check input errors before inserting in database
- if(empty($username_err) && empty($name_err) && empty($phone_err) && empty($email_err) && empty($password_err) && empty($confirm_password_err)){
- // Prepare an insert statement
- $sql = "INSERT INTO users (username, name, phone, email, password) VALUES (?, ?, ?, ?, ?)";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "sssss", $param_username, $param_name, $param_phone, $param_email, $param_password);
- // Set parameters
- $param_username = $username;
- $param_name = $name;
- $param_phone = $phone;
- $param_email = $email;
- $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- // Redirect to login page
- header("location: login.php");
- } else{
- echo "Something went wrong. Please try again laterp.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Close connection
- mysqli_close($link);
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Sign Up Form by Colorlib</title>
- <!-- Font Icon -->
- <link rel="stylesheet" href="fonts/material-icon/css/material-design-iconic-font.min.css">
- <!-- Main css -->
- <link rel="stylesheet" href="css/style.css">
- </head>
- <body>
- <div class="main">
- <section class="signup">
- <!-- <img src="images/signup-bg.jpg" alt=""> -->
- <div class="container">
- <div class="signup-content">
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" id="signup-form" class="signup-form">
- <h2 class="form-title">Create account</h2>
- <div class="form-group">
- <input type="text" class="form-input" name="name" id="name" placeholder="Nombre" value="<?php echo $name; ?>">
- </div>
- <div class="form-group">
- <input type="email" class="form-input" name="email" id="email" placeholder="Correo electronico" value="<?php echo $email; ?>">
- </div>
- <div class="form-group">
- <input type="phone" class="form-input" name="phone" id="phone" placeholder="Telefono" value="<?php echo $phone; ?>">
- </div>
- <div class="form-group">
- <input type="text" class="form-input" name="username" id="username" placeholder="Usuario" value="<?php echo $username; ?>">
- </div>
- <div class="form-group">
- <input type="text" class="form-input" name="password" id="password" placeholder="Password" value="<?php echo $password; ?>">
- <span toggle="#password" class="zmdi zmdi-eye field-icon toggle-password"></span>
- </div>
- <div class="form-group">
- <input type="password" class="form-input" name="confirm_password" id="re_password" placeholder="Repeat your password" value="<?php echo $confirm_password; ?>">
- </div>
- <div class="form-group">
- <input type="submit" name="submit" id="submit" class="form-submit" value="Sign up"/>
- </div>
- </form>
- <p class="loginhere">
- Have already an account ? <a href="#" class="loginhere-link">Login here</a>
- </p>
- </div>
- </div>
- </section>
- </div>
- <!-- JS -->
- <script src="vendor/jquery/jquery.min.js"></script>
- <script src="js/main.js"></script>
- </body><!-- This templates was made by Colorlib (https://colorlib.com) -->
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement