PHP form Registration script with mysql database entry

PHP form Registration script with MySQL database

PHP form Registration script with Database [mysql]  is a requirement of every web app that takes user data and uses it for further processing according to the need.

I created such form with PHP MySQL database which can be copied for use from below given lines.This code script works properly with PHP 7.1 and MySql5. I created it on my local machine running on Windows.But for a live server, I always prefer UBUNTU Linux.I moved from MySql_* function to PDO extension so don’t worry about it, it going to work fine if you use old MySql_* functions or MySqli extension.

It is the first step to creating a Registration form for your very first web application.When it comes to PHP I called it PHP registration form.If you want to add file upload functionality to the registration from then get PHP file upload code with database script and modify according to your requirement.

PHP form registration script with MySql Database requirements:

  1. HTML Form
  2. MySql Database
  3. PHP Registration Script
  4. DataCheck PHP script

Getting started: Step1- Create an HTML Form For Registration

Every Registration form required HTML form.Create an HTML form with Method POST.POST method is more secure than GET method because no visible information appears in browser’s address bar.Add action, Register.php[ registration script]. Register.php handle the form and process data for make entries into the database. below is a given form code you can copy.

<form method="post" action="index.php">
Email:<input type="email" name="email1" placeholder="[email protected]"><br>
Re Enter Email: <input type="email" name="email2" placeholder="[email protected]"><br>
Name: <input type="text" name="name" placeholder="Enter your Name"><br>
Password: <input type="Password" name="Password" placeholder="***************"><br>
 <input type="submit" name="go" value='SignUP'>
</form>

 Step2: Create a MySql Database [test]

This script required MySql database with PDO Extension. It is not required you can do it by simply using MySqli or MySql_*functions. For This script, I am using database name “test” on my local machine.I use table “users” from test database. I have exported my table to users.sql for better understanding. You can import bellow given users.sql to your own database.

Users.sql

-- phpMyAdmin SQL Dump
-- version 4.6.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jun 02, 2017 at 12:02 PM
-- Server version: 5.7.14
-- PHP Version: 5.6.25

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
 `id` int(10) NOT NULL,
 `email` varchar(250) NOT NULL,
 `password` varchar(50) NOT NULL,
 `name` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `email`, `password`, `name`) VALUES
(1, '[email protected]', 'admin', 'admin'),
(2, '[email protected]', 'password', 'admin'),
(6, '[email protected]', 'password', 'admin'),
(7, '[email protected]', 'password', 'admin'),
(8, '[email protected]', 'password', 'admin'),
(9, '[email protected]', 'password', 'admin');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
 ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
 MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION */;

Step3: PHP form registration script

I assume that you have created Database and imported table users.sql to your own MySql database. Now you are ready to create register.php That will handle all your form data and process it for making entries to the database.Recheck your HTML form and make sure your action is set to register.php.You can create a copy of register.php script from bellow given code.

Register.php

<?php
session_start();
error_reporting(0);
$email1=$_POST['email1'];
$email2=$_POST['email2'];
$Name=$_POST['name'];
$Password=$_POST['Password'];
$go=$_POST['go'];
if(isset($go)){
 if ($email1!="" and $email2!="" and $Name!="" and $Password!="") {
 if ($email1===$email2) {
 $db = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
 $stmt = $db->query("SELECT * FROM users where email='$email2'");
 $row = $stmt->rowCount();
 if ($row > 0){
 $message="Email is alrady registered with us try logging in.";
 }
 else{
 $stmt = $db->prepare("INSERT INTO users(email,password,name) VALUES(:field1,:field2,:field3)");
 $stmt->execute(array(':field1' => $email1, ':field2' => $Password, ':field3' => $Name));

 $message="<h1 style='color:green'>You are successfully registered with US lets go to user.php</h1>";
 $_SESSION['user']="$email1";
 header( "refresh:2;url=user.php" );
 }

 }
 else{
 $message="Email miss match.. Kindly type email carefully";
 }
 }else{
 $message="please Fill all form fields";
 }
}


?>
<?php echo "<h1>$message </h1>"; ?>

 

The first function is used for the session, if we get all the condition true then we store user id or email to the session  for sending users on user account page.All the data is collected by using POST method.We check fields for empty strings and then make sure that both entered email are same and not already present in the database. If all the condition meets true, the script store email to the session and create an entry to the database regarding submitted information.

Step4: Data check PHP script

Datacheck PHP script is just for checking all data from your database.It checks that Register.php working fine or not. IT puts all table data in the form of HTML table.

check.php

<?php
session_start();
//error_reporting(0);
echo "<table>";
echo "<th> <tr> <td><b>#ID</b> </td><td><b>Email</b> </td><td><b>Name</b></td><td><b>Password</b> </td> </tr> </th>";
$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
 $stmt = $db->query("SELECT * FROM users");
 $result=$stmt->setFetchMode(PDO::FETCH_ASSOC); // ok with value 1
 foreach($stmt->fetchAll() as $v){
 $id=$v['id'];
 $email=$v['email'];
 $name=$v['name'];
 $pw="N/A";
 echo "<tr> <td>$id </td><td>$email </td><td>$name</td><td>$pw </td> </tr>";

 }
 
 
 echo "</table>"; 
 

?>

This is all done!

Read also: PHP file upload script with database and gallery

Get any problem regarding these codes? let’s discuss here bellow into the comment box.