Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- ini_set("display_errors", 1);
- // Include config file
- // Define variables and initialize with empty values
- $username_err = $password_err = $confirm_password_err = $passwordConf_err = $email_err = $email_used_err = "";
- $username = $password = $email = "";
- $param_password = $param_username =$param_email = "";
- $passwordConf = "";
- // 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{
- require_once "db_conn.php";
- // Prepare a select statement
- $sql = "SELECT id FROM estiweb_db 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 later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- //validate email
- if(empty(trim($_POST["email"]))){
- $email_err = "Please fill in the Email!";
- } else{
- require_once "db_conn.php";
- // Prepare a select statement
- $sql = "SELECT id FROM estiweb_db 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_used_err = "This email is already in use!";
- } else{
- $email = trim($_POST["email"]);
- }
- } else{
- echo "Oops! Something went wrong. Please try again later.";
- }
- // 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["passwordConf"]))){
- $passwordConf_err = "Please confirm password.";
- } else{
- $passwordConf = trim($_POST["passwordConf"]);
- if(empty($password_err) && ($password != $passwordConf)){
- $confirm_password_err = "Password did not match.";
- }
- }
- /*
- echo $username_err;
- echo $password_err;
- echo $email_err;
- */
- // Check input errors before inserting in database
- if(empty($username_err) && empty($password_err) && empty($email_err) && empty($confirm_password_err) && empty($passwordConf_err)){
- require_once "db_conn.php";
- // Prepare an insert statement
- $sql = mysqli_query("INSERT INTO estiweb_db (username, email, password) VALUES (? ,?, ?)");
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "sss", $param_username, $param_email, $param_password);
- // Set parameters
- $param_username = $username;
- $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
- $param_email = $email;
- // Attempt to execute the prepared statement
- if ($link->query($stmt) === TRUE) {
- //header("location: ../login.php")
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- } else{
- echo "Something went wrong. Please try again later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- // Close connection
- mysqli_close($link);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement