Dear Reader, Its very common and basic requirement for any mobile, desktop or laptop users to register themselves to any application or software for accessing its features/functionalities. For this user will be encountered with software’s Registration Page at first. Hence we are going to learn about the creation of ‘Registration Module creation using MySQL, PHP’.
For the creation of Registration Module, we need to create Table in Database.
Let us create a Database named, ‘web_db‘ and Table named, ‘users‘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// `users` Table structure CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name1` varchar(100) NOT NULL, `name2` varchar(100), `email` varchar(255) NOT NULL, `mobile` varchar(15) NOT NULL, `password` varchar(255) NOT NULL, `status` ENUM ('0','1') NOT NULL DEFAULT '0', `registration_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; |
Now before going to start to code for Registration Page, we need to establish a connection between Database to PHP file, so that we could insert the user entries into the DB Table.
For DB Connection, you need to create a php page, named :- db_connect.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php define('HOSTNAME','localhost'); define('USERNAME','root'); define('PASSWORD',''); define('DBNAME','web_db'); $conn = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DBNAME); if(!$conn) // Check connection { die("Connection failed: " . mysqli_connect_error()); } else { //echo 'Connection Established'; } |
Now you need to create ‘registration.php‘.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
<?php ob_start(); session_start(); if( isset($_SESSION['user_id'])!="" ) { header("Location: home.php"); } include 'db_connect.php'; $error = false; if ( isset($_POST['save']) ) { // SQL injections prevention in user's input $name1 = trim($_POST['name1']); $name1 = strip_tags($name1); $name1 = htmlspecialchars($name1); if($_POST['name2'] != '') { $name2 = trim($_POST['name2']); $name2 = strip_tags($name2); $name2 = htmlspecialchars($name2); } $email = trim($_POST['email']); $email = strip_tags($email); $email = htmlspecialchars($email); $mobile = trim($_POST['mobile']); $mobile = strip_tags($mobile); $mobile = htmlspecialchars($mobile); $pass1 = trim($_POST['password_1']); $pass1 = strip_tags($pass1); $pass1 = htmlspecialchars($pass1); $pass2 = trim($_POST['password_2']); $pass2 = strip_tags($pass2); $pass2 = htmlspecialchars($pass2); // Name Validation if (empty($name1)) { $error = true; $name1Error = "Please enter your first name."; } else if (strlen($name1) < 3) { $error = true; $name1Error = "First Name must have atleast 3 characters."; } else if (!preg_match("/^[a-zA-Z ]+$/",$name1)) { $error = true; $name1Error = "First Name must contain alphabets and space."; } // Email Validation if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) { $error = true; $emailError = "Please enter valid email address."; } else { // check email exist or not $query = "SELECT email FROM users WHERE email='$email'"; $result = mysqli_query($conn,$query); $count = mysqli_num_rows($result); if($count!=0){ $error = true; $emailError = "Provided Email is already in use."; } } // Mobile No. Validation if (empty($mobile)) { $error = true; $mobileError = "Please enter your Mobile No."; } else if (strlen($mobile) < 10) { $error = true; $mobileError = "Mobile No. must have atleast 10 digits."; } else if (!preg_match("/^(\+\d{1,3}[- ]?)?\d{10}$/",$mobile)) { $error = true; $mobileError = "Mobile No. must be in its proper format"; } // Password Validation if (empty($pass1)){ $error = true; $pass1Error = "Please enter password."; } else if(strlen($pass1) < 6) { $error = true; $pass1Error = "Password must have atleast 6 characters."; } // Confirm Password Validation if (empty($pass2)) { $error = true; $pass2Error = "Please enter confirm password."; } else if(strlen($pass2) < 6) { $error = true; $pass2Error = "Confirm Password must have atleast 6 characters."; } else if($pass1 != $pass2) { $error = true; $pass2Error = "Password and Confirm Password must be of same value."; } // md5() encryption of the password before saving in the database $password = md5($pass1); // if there's no error, continue to signup if( !$error ) { //$registration_date = date('Y-m-d H:i:s'); $insert_query = "INSERT INTO users(name1,name2,email,mobile,password,status,registration_date) VALUES('$name1','$name2','$email','$mobile','$password','0',NOW())"; //echo $insert_query; $result_insert = mysqli_query($conn,$insert_query); if ($result_insert ) { $errTyp = "success"; $errMSG = "Successfully registered, you may login now"; unset($name); unset($email); unset($mobile); unset($pass1); unset($pass2); } else { $errTyp = "danger"; $errMSG = "Something went wrong, try again later..."; } } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Registration Module using MySQL, PHP</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" /> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div class="container"> <div id="login-form"> <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off"> <div class="col-md-12"> <div class="form-group"> <h2 class="">Sign Up.</h2> </div> <div class="form-group"> <hr /> </div> <?php if ( isset($errMSG) ) { ?> <div class="form-group"> <div class="alert alert-<?php echo ($errTyp=="success") ? "success" : $errTyp; ?>"> <span class="glyphicon glyphicon-info-sign"></span> <?php echo $errMSG; ?> </div> </div> <?php } ?> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"> <span class="glyphicon glyphicon-user"></span> </span> <input type="text" name="name1" class="form-control" placeholder="Enter First Name" maxlength="50" value="<?php echo $name1 ?>" /> </div> <span class="text-danger"><?php echo $name1Error; ?></span> </div> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span> <input type="text" name="name2" class="form-control" placeholder="Enter Last Name" maxlength="50" value="<?php echo $name2 ?>" /> </div> <!--<span class="text-danger"><?php //echo $name2Error; ?></span>--> </div> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span> <input type="email" name="email" class="form-control" placeholder="Enter Your Email" maxlength="40" value="<?php echo $email; ?>" /> </div> <span class="text-danger"><?php echo $emailError; ?></span> </div> <div class="form-group"> <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-phone"></span></span> <input type="text" name="mobile" class="form-control" placeholder="Enter Your Mobile No" maxlength="10" value="<?php echo $mobile; ?>" /> </div> <span class="text-danger"><?php echo $mobileError; ?></span> </div> <div class="form-group"> <div class="input-group"> <!--<label>Password</label>--> <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span> <input type="password" name="password_1" class="form-control" placeholder="Enter Password" maxlength="15" /> </div> <span class="text-danger"><?php echo $pass1Error; ?></span> </div> <div class="form-group"> <div class="input-group"> <!--<label>Confirm password</label>--> <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span> <input type="password" name="password_2" class="form-control" placeholder="Enter Confirm Password" maxlength="15" /> </div> <span class="text-danger"><?php echo $pass2Error; ?></span> </div> <div class="form-group"> <hr /> </div> <div class="form-group"> <button type="submit" class="btn btn-block btn-primary" name="save">Sign Up</button> </div> <div class="form-group"> <hr /> </div> <div class="form-group"> Already a Member ? <a href="user_login.php">Sign in</a> </div> </div> </form> </div> </div> </body> </html> <?php ob_end_flush(); ?> |
Congratulations you have successfully Completed Registration Module using MySQL, PHP. In next article, we will learn about Login Module using MySQL, PHP. For now, If this post was helpful for you then please share this post on social media and if you have any query then please contact us or comment below and don’t forget us follow and like on Social Media, Thanks
3 Comments:
terwixonse May 12, 2019
Help. I am looking for guide
Webpreparations Team May 15, 2019
Hello,
Please tell us
terwixonse May 17, 2019
I am from Slovenia. I can help with build this forum. Thanks for approved.
Jaz sem Slovenka. Lahko pomagam pri razvoju foruma.