Merge form insert to dashboard PHP Login

Merge form insert to dashboard PHP Login

Step 1: MySQL - Database Table Creation

  1. Create Users Table (users table):

    CREATE TABLE users ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, email VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP );
  2. Create Employees Table (employees table):

    CREATE TABLE employees ( id INT(11) NOT NULL, name VARCHAR(100) DEFAULT NULL, position VARCHAR(255) DEFAULT NULL, age VARCHAR(255) DEFAULT NULL, office VARCHAR(255) DEFAULT NULL, start_date DATETIME DEFAULT NULL, salary INT(10) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Step 2: PHP Database Connection (config.php)

This script establishes a connection to the MySQL database.

<?php define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', '123456'); define('DB_NAME', 'login_system'); $conection_db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); if($conection_db === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } ?>

Step 3: Registration Logic (php_register.php)

This file handles the form submission for user registration. It validates the inputs and checks if the email already exists. If no errors are found, it inserts the new user into the users table.

<?php $email = $password = $confirm_password = ""; $email_err = $password_err = $confirm_password_err = ""; if($_SERVER["REQUEST_METHOD"] == "POST") { if(empty(trim($_POST["email"]))) { $email_err = "Please enter a email."; } else { $sql = "SELECT id FROM users WHERE email = ?"; if($stmt = mysqli_prepare($conection_db, $sql)) { mysqli_stmt_bind_param($stmt, "s", $param_email); $param_email = trim($_POST["email"]); if(mysqli_stmt_execute($stmt)) { mysqli_stmt_store_result($stmt); if(mysqli_stmt_num_rows($stmt) == 1) { $email_err = "This email is already taken."; } else { $email = trim($_POST["email"]); } } mysqli_stmt_close($stmt); } } 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"]); } 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."; } } if(empty($email_err) && empty($password_err) && empty($confirm_password_err)) { $sql = "INSERT INTO users (email, password) VALUES (?, ?)"; if($stmt = mysqli_prepare($conection_db, $sql)) { mysqli_stmt_bind_param($stmt, "ss", $param_email, $param_password); $param_email = $email; $param_password = password_hash($password, PASSWORD_DEFAULT); if(mysqli_stmt_execute($stmt)) { header("location: login.php"); } else { echo "Something went wrong. Please try again later."; } mysqli_stmt_close($stmt); } } mysqli_close($conection_db); } ?>

Step 4: Sign-Up Form (sign-up.html)

The sign-up form allows users to enter their email and password and confirm the password.

<form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <!-- Email --> <input type="email" name="email" placeholder="Email" value="<?= $email; ?>"> <span><?= $email_err; ?></span> <!-- Password --> <input type="password" name="password" placeholder="Password" value="<?= $password; ?>"> <span><?= $password_err; ?></span> <!-- Confirm Password --> <input type="password" name="confirm_password" placeholder="Confirm Password" value="<?= $confirm_password; ?>"> <span><?= $confirm_password_err; ?></span> <button type="submit">Signup</button> </form>

Step 5: Login Form (login.php)

The login form allows users to log in using their email and password.

<?php session_start(); if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){ header("location: welcome.php"); exit; } require_once "config.php"; require_once "php_login.php"; ?> <form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <!-- Email --> <input type="email" name="email" placeholder="Email" value="<?= $email; ?>"> <span><?= $email_err; ?></span> <!-- Password --> <input type="password" name="password" placeholder="Password"> <span><?= $password_err; ?></span> <button type="submit">Signin</button> </form>

Step 6: PHP Login Handler (php_login.php)

This file processes the login request and validates the email and password.

<?php $email = $password = ""; $email_err = $password_err = ""; if($_SERVER["REQUEST_METHOD"] == "POST") { if(empty(trim($_POST["email"]))) { $email_err = "Please enter email."; } else { $email = trim($_POST["email"]); } if(empty(trim($_POST["password"]))) { $password_err = "Please enter your password."; } else { $password = trim($_POST["password"]); } if(empty($email_err) && empty($password_err)) { $sql = "SELECT id, email, password FROM users WHERE email = ?"; if($stmt = mysqli_prepare($conection_db, $sql)) { mysqli_stmt_bind_param($stmt, "s", $param_email); $param_email = $email; if(mysqli_stmt_execute($stmt)) { mysqli_stmt_store_result($stmt); if(mysqli_stmt_num_rows($stmt) == 1) { mysqli_stmt_bind_result($stmt, $id, $email, $hashed_password); if(mysqli_stmt_fetch($stmt)) { if(password_verify($password, $hashed_password)) { session_start(); $_SESSION["loggedin"] = true; $_SESSION["id"] = $id; $_SESSION["email"] = $email; header("location: welcome.php"); } else { $password_err = "The password you entered was not valid."; } } } else { $email_err = "No account found with that email."; } } mysqli_stmt_close($stmt); } } mysqli_close($conection_db); } ?>

Step 7: Welcome Page (welcome.php)

This page shows a welcome message and allows the user to log out.

<?php session_start(); if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){ header("location: login.php"); exit; } ?> <p>Welcome, <?= $_SESSION['email']; ?></p> <a href="logout.php">Logout</a>

Step 8: Logout (logout.php)

This script handles logging out the user by destroying the session.

<?php session_start(); $_SESSION = array(); session_destroy(); header("location: login.php"); exit; ?>

This PHP and MySQL code covers the basic registration and login system. Each step is designed to handle user input validation, session management, and error reporting effectively.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

1 Comments

CAN FEEDBACK
  1. Anonymous
    Anonymous
    Ganda goods to haha
close