top of page

Creating PHP Form using MySQL With Validation



Task-1: Using Php and MySQl

1. For the same form as in Task-1 of assignment-2, You have to now create a Database and store the entered information into that.(3 marks)

2. For example ,enter the information of at least users to be stored into the DB.

(2 marks)

3. Next step is to query from the database(i.e. record of a user) and display it in the s same form fields. You can design another form for this purpose.

(a) for example , You want to retrieve record of a user with First name as "Amit".

You enter the name "Amit" in the First Name of your text field. If that name exists in the data base then all of its other information(last name, email, phone) shall be displayed in the respective fields else display the message "user with that name do not exist. try again" (1 mark)

Note : When the search retrieves records then display records in Tabular form.



PHP Scripts(assignment2.php)

<!DOCTYPE html>

<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Registration Form</title>
 
 <script>
 function Validate(pass) {
 var regex_phone = /^\(?[0-9]{3}\)?-\(?[0-9]{3}\)?-\(?[0-9]{4}\)?$/
 var regex_email = /^[A-Za-z0-9]+@[A-Za-z0-9]+\.com$/
 var regex_pwd = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,}$/
 var regex_name=/^[a-zA-Z]+$/
 //<!--/^[a-zA-Z0-9!"#\$%&'\(\)\*\+,-\.\/:;<=>\?@[\]\^_`\{\|}~]{8,}$/--!>

 if(reg_form.FName.value == "" | reg_form.FName.value == null)
            {
 alert("First Name cannot be empty!")
 reg_form.FName.focus();
 return false;
            }
 if(!regex_name.test(reg_form.FName.value))
            {
 alert("First name should not contains special character");
 reg_form.FName.focus()
 return false;
            }
 if (reg_form.FName.value.length < 4) {
 alert("First Name must be minimum of 4 length")
 reg_form.FName.focus();
 return false;

            }
 
 if (reg_form.LName.value == "" | reg_form.LName.value == null) {
 alert("Last Name cannot be empty!")
 reg_form.LName.focus();
 return false;
            }
 if(!regex_name.test(reg_form.LName.value))
            {
 alert("Last name should not contains special character");
 reg_form.LName.focus()
 return false;
            }
 if (reg_form.LName.value.length < 4) {
 alert("Last Name must be minimum of 4 length")
 reg_form.FName.focus();
 return false;
            }
 if (reg_form.phoneNumb.value == "" | reg_form.phoneNumb.value == null) {
 alert("Phone Number cannot be empty!")
 reg_form.phoneNumb.focus();
 return false;
            }
 if(!regex_phone.test(reg_form.phoneNumb.value))
            {
 alert("The phone number must of format (xxx)-(xxx)-(xxxx) or xxx-xxx-xxxx")
 reg_form.phoneNumb.focus();
 return false;
            }
 if (reg_form.Email.value == "" | reg_form.Email.value == null) {
 alert("Email cannot be empty!")
 reg_form.Email.focus();
 return false;
            }

 if(!regex_email.test(reg_form.Email.value))
            {
 alert("Email must contain @ and .com")
 reg_form.Email.focus()
 return false;
            }

 if (reg_form.pwd.value == "" | reg_form.pwd.value == null) {
 alert("Password cannot be empty!")
 reg_form.pwd.focus();
 return false;
            }
 if(pass.length < 8)
            {
 alert("Password must of at least 8 length!")
 reg_form.pwd.focus();
 return false;
            }
 if(!regex_pwd.test(reg_form.pwd.value))
            {
 alert("Password must be minimum 8 characters in length with uppercase and lowercase, number and a special character")
 reg_form.pwd.focus();
 return false;
            }
 if (reg_form.confirmPwd.value == "" | reg_form.confirmPwd.value == null) {
 alert("Confirm Password cannot be empty!")
 reg_form.confirmPwd.focus();
 return false;
            }
 if(reg_form.pwd.value!=reg_form.confirmPwd.value){
 alert("The confirm password does not same as password")
 reg_form.confirmPwd.focus();
 return false;
            }
 
 if(!reg_form.checkbox.checked) {
 alert("Please indicate that you accept the Terms and Conditions");
 reg_form.checkbox.focus();
 return false;
            }
 
 reg_form.submit();

        }
 </script>
 
 <style>
 body{
 background-color:#fbd5ae;
 border-width: 3px;
 border-color:#f9b978;
 line-height:30px;
        }
 div.a{
 background: orange;  
 color:white;
 text-align: left;
 padding-left:10px;
        }
 div.b{
 background: #00003f; 
 color: orange;
 text-align: right;
 align-items: center;
        }
 table{
 color:orange;
 width: auto;
        }
 td.input{
 text-align:left;
 
        }
 input{
 margin-left: 15px;
        }
 select{
 margin-left: 15px;
        }
 </style>

</head>

<body>
<div class="a">Sign up</div>
<div class="b"> 

<form name = "reg_form" action="register_form.php" method = "post"> 
<!--Sign up table-->
<table>
<tbody>
 <tr>
 <td>First Name:*</td>
 <td class="input"><input type="text" name="FName" placeholder="Enter First Name" autocomplete="off" /></td>
 <td class="input"> Minimum 4 characters in length. Should not contains special character</td>
 </tr>
 
 <tr>
 <td>Last Name:*</td>
 <td class="input"><input type="text" name="LName" placeholder="Enter Last Name" autocomplete="off" /></td>
 <td class="input"> Minimum 4 characters in length. Should not contains special character</td>
 </tr>
 
 <tr>
 <td>Phone:*</td>
 <td class="input"><input type="text" name="phoneNumb" placeholder="Phone number" autocomplete="off" /></td>
 <td class="input"> Phone number must has format (xxx)-(xxx)-(xxxx) or xxx-xxx-xxxx</td>
 </tr>
 
 <tr>
 <td>Email:*</td>
 <td class="input"><input type="email" name="Email" autocomplete="off" /></td>
 <td class="input">Email must contain @ and .com</td>
 </tr>
 
 <tr>
 <td>Password:*</td>
 <td class="input"><input type="password" name="pwd" autocomplete="off" /></td>
 <td class="input">Password must have at least 8 characters including 1 lowerletter, 1 upperletter, 1 number, and 1 special symbols</td>
 </tr>
 
 <tr>
 <td>Confirm Password:*</td>
 <td class="input"><input type="password" name="confirmPwd" autocomplete="off" /></td>
 <td class="input">Confirm password must be same as password</td>
 </tr>
 <tr>
 <td>
 <td class="input"><input type="checkbox" name="checkbox" />I agree to the Terms of use</td>
 </tr>

</tbody>
</table></div>

<!--Submit and cancel button-->
<div style="background: orange; padding-left: 50px;" align="right">
 <!--<button style="color: white; background: green; height: 35px; border-radius: 3px;" onclick="Validate(reg_form.pwd.value)"> Submit Query </button> -->
 <input type="button" name="btn_submit" value="Submit Query" onclick="Validate(reg_form.pwd.value)" style="color: white; background: green; height: 35px; border-radius: 3px;"/>
 <button style="color: white; background: red; height: 35px; border-radius: 3px; width: 80px;"> Cancel </button>
 </div>
 
</form>
</body>
</html>
<?php
include('fetch_records.php');

?>

PHP Scripts(register.php)

<?php
include('db.php');
?>
 <?php

 $FName = $_POST['FName'];
 $LName = $_POST['LName'];
 $phoneNumb = $_POST['phoneNumb'];
 $Email = $_POST['Email'];
 $pwd = $_POST['pwd'];
 $confirmPwd = $_POST['confirmPwd'];
 $checkbox = $_POST['checkbox'];

 
 $sql = "insert into mytable (fname, lname,phone,email,pass,confirm_pass,checkbox)
                values ('$FName', '$LName', '$phoneNumb','$Email','$pwd','$confirmPwd','$checkbox')";

 if (mysqli_query($con, $sql)) {
 echo "<script>alert('New records created successfully')
                    </script>";
 header('location:assignment2.php');
 
                } else {
 echo "Error: " . $sql . "<br>" . mysqli_error($con);
                }
 mysqli_close($con);
 
?>


PHP Scripts(db.php)

<?PHP
$con = mysqli_connect("localhost","root","","form_db");
if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 exit();
  }
 else 
 //echo "Success to connect to MySQL ";
?>

PHP Scripts(fetch_records.php)

<?php
include('db.php');
?>
<html>
<head> 
<style>
#customers {
 font-family: Arial, Helvetica, sans-serif;
 border-collapse: collapse;
 width: 50%;
 margin-left:25%;
 margin-top:2%;

}

#customers td, #customers th {
 border: 1px solid blue;
 text-align: center;
 padding: 4px;

}
#customers tr{
 background-color: #00003f;
 color: orange;
}

#customers tr:nth-child(even){background-color: #f2f2f2;color: #00003f;}

#customers tr:hover {background-color: #8000ff;}

#customers th {
 padding-top: 8px;
 padding-bottom: 8px;
 text-align: center;
 background-color: orange;
 color: white;
}

</style> 
</head>
<body>
<div align="center">
<form method = "post" >
 <input type="text" name="FName" placeholder="search first name" autocomplete="off"/>
 <input type="submit" name ="submit" value="Search">
</form>
</div>
</body>
</html>
<?php
if ( isset( $_POST['submit'] ) ) 
{
 $FName = $_POST['FName'];

 $sql = "SELECT * FROM mytable where fname='$FName'";
 
 $result = $con->query($sql);

echo "<table id='customers'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Mobile</th>
<th>Email</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo " </table>";
mysqli_close($con);
}
?>


Contact Us to get any PHP programming help, PHP project help, Or other HTML, CSS project help with an affordable price at contact@codersarts.com and get instant help .

bottom of page