PHP Register Form
From w3cyberlearnings
Contents |
PHP Registration form
This is a sample registration form that user provides his or her information. All the user inputs within the form will be saved in the MySQL database.
Table: registration table
CREATE TABLE `register` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(40) DEFAULT NULL, `middle_name` varchar(40) DEFAULT NULL, `last_name` varchar(40) DEFAULT NULL, `phone` varchar(10) DEFAULT NULL, `login_name` varchar(40) DEFAULT NULL, `password` varchar(40) DEFAULT NULL, PRIMARY KEY (`id`) )
Step 1(registration.php)
<?php $msg = ""; if (!empty($_POST['register'])) { // connect to the MySQL database $mysqli_db = new mysqli('localhost', 'root', 'yeething', 'my_db'); // get form field data $first_name = $_POST['first_name']; $middle_name = $_POST['middle_name']; $last_name = $_POST['last_name']; $phone = $_POST['phone']; $login_name = $_POST['login_name']; $login_password1 = $_POST['login_password1']; $login_password2 = $_POST['login_password2']; // sql statement for insert into the table $query = 'INSERT INTO register(first_name, middle_name, last_name, phone,login_name,password) VALUES(?,?,?,?,?,?)'; // binding the variable to for insert $requery = $mysqli_db->prepare($query); $requery->bind_param("ssssss", $first_name, $middle_name, $last_name, $phone, $login_name, $login_password1); // execute the query and insert into the table, // if the insert is successful, it will redirect to the home.php page if ($requery->execute()) { header('location:home.php?id=' . $requery->insert_id); } // close the mysql database connection $mysqli_db->close(); } else { // if user submit the form without any input fields $msg = 'Please enter your personal information!<br/>'; } ?> <html> <head> <title>User Register</title> </head> <body> <b><?php echo $msg; ?></b> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table border="0"> <tr> <td>First Name:</td> <td> <input type="text" name="first_name" size="20" maxlength="20"/> </td> </tr> <tr> <td>Middle Name:</td> <td><input type="text" name="middle_name" size="20" maxlength="20" /></td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="last_name" size="20" maxlength="20" /></td> </tr> <tr> <td>Tel/Phone:</td> <td><input type="text" name="phone" size="20" maxlength="20" /></td> </tr> <tr> <td>Login Name:</td> <td><input type="text" name="login_name" size="20" maxlength="20" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="login_password1" size="20" maxlength="20" /></td> </tr> <tr> <td>Confirm:</td> <td><input type="password" name="login_password2" size="20" maxlength="20" /></td> </tr> <tr> <td colspan="2"><input type="submit" name="register" value="Register" /></td> </tr> </table> </form> </body> </html>
Step 2(home.php)
<html> <head> <title>Home</title> </head> <body> <?php echo 'Welcome !!! <br/>'; $mysqli_db = new mysqli('localhost', 'root', 'yeething', 'my_db'); $query = 'SELECT * FROM register WHERE id=' . $_GET['id']; $myquery = $mysqli_db->query($query); $rows = $myquery->fetch_assoc(); foreach ($rows as $k => $v) { echo $k . '-->' . $v . '<br/>'; } $mysqli_db->close(); ?> </body> </html>
Output
Related Links
HTML Form