In this post i will show you how to create a basic registration page and store those values in the database and also do some error checking like empty values are not allowed. Here i will use xampp as a server and create a database there with 4 columns username , password, gender and name. These are the values we will store in our database name sample and table name users. |
Now open a file in any text editor and place the following code :
<!DOCTYPE HTML> <html> <head> </head> <body> <?php $servername = "127.0.0.1"; $username = "codingcorner"; $password = "yourPassword"; $nameErr = $unameErr = $genderErr = $pwdErr = ""; $name = $uname = $gender = $comment = $pwd = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); } if (empty($_POST["uname"])) { $unameErr = "Username is required"; } else { $uname = test_input($_POST["uname"]); } if (empty($_POST["pwd"])) { $pwdErr= "Password is required"; } else { $pwd = test_input($_POST["pwd"]); } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "Gender is required"; } else { $gender = test_input($_POST["gender"]); } } try { $conn = new PDO("mysql:host=$servername;dbname=sample", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO users (username, password , gender , name) VALUES ('$uname', '$pwd','$gender','$name')"; // use exec() because no results are returned $conn->exec($sql); echo "Welcome !"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>A Simple Registration Form</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> Name: <input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span> <br><br> Username: <input type="text" name="uname"> <span class="error">* <?php echo $unameErr;?></span> <br><br> Password: <input type="password" name="pwd"> <span class="error">*<?php echo $pwdErr;?></span> <br><br> Comment: <textarea name="comment" rows="5" cols="40"></textarea> <br><br> Gender: <input type="radio" name="gender" value="female">Female <input type="radio" name="gender" value="male">Male <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>Your Details:</h2>"; echo $name; echo "<br>"; echo $uname; echo "<br>"; echo $pwd; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>Now save this file as registration-tuts.php or any other name you wish in C:\xampp\htdocs or any path where your xampp is installed .
Start Apache and Mysql in your xampp control panel.
It looks something like this
now the page we just created will open
OUTPUT :
When the * mark (required fields ) are not filled it shows an error telling this field is required .
When all the fields are filled .
Now we will check the database we created in the beginning for the details entered here.
In the browser go to localhost/phpmyadmin and then in the right side select your database and table , then it will show you the details you entered in the form we created.
That's it ! We are done with our registration form.