Part 2- Storing form data into the database
In Part 1, you see how to perform a simple form validation using PHP. This is the continuation of the Part 1-Form Validation. After filling the form field, user clicks the submit button. PHP validates form and transfer user entered data to database and redirect the user to a greeting page. For this, we need to create a database using MySQL.
Let's create the database. You must have WAMP in your system. Start the WAMP and, go to phpmyadmin page, login with your credentials.
Then click new on left top. See this image
Now give the database a name and click create. Next create table by entering the table name in the field provided and select number of column you need in your table and click GO. Now in the next page enter column name, Type,Length,etc and click save
After creating the database, write the PHP code for creating connection
with our registration form and database just created. Check the below code
<?php
//connecting to Database
$con=mysqli_connect('localhost','<root login name>','<root password here>','<databse name here>');
//checking connection established or not
if(!$con)
die("Cannot connect to database");
//selecting the active/required database for this form
$my_db=mysqli_select_db($con,'<databse name here>');
//checking succeful or not
if(!$my_db)
die('Database not selected');
//session started
session_start();
//form datas are storing into php variables
$name=$_POST['lname'];
$psw=$_POST['lpass'];
$gen=$_POST["gender"];
$mail=$_POST['lmail'];
//checking if the submitted values are already in database
$sqlsel="SELECT * FROM registery WHERE name='$name' or mail='$mail'";
$sel=mysqli_query($con,$sqlsel) or die(mysqli_error($con));//querying db
$count=mysqli_num_rows($sel);
if($count==1){
header('location:form_reg.php?msg=already entered');// redirecting to previouse page with error message
}else{
// insertion
$sqlin="INSERT INTO registery(name,mail,pass)VALUES('".$name."','".$mail."','".$psw."')";
//checking if the insertion successeful or not
$in=mysqli_query($con,$sqlin) or die(mysqli_error($con));
//after succeful insertion the page will redirect you to a welcome page
if($in){
$_SESSION['lname']=$name;
header('location:welcome.php');
}else{echo 'failed insertion';}
}
?>
You can see that this code contains creation of connection to the database using mysqli_connect(), insertion operation, selection operation.
Now, we should create a greeting page to redirect the user on successful submission.
See the code below.
<?php
session_start();
?>
<html>
<head>
<title> Welcome</title>
</head>
<body>
<?php
echo "Thank you, ".$_SESSION['lname'];
?><br/>
<p>
your data submitted succefully</p></body>
</html>
Now for the last part, after filling the form if there is a wrong entry in a field, then clicking the submit button will redirect you to the same page with all field left blank. There need again user to fill the enitre form. To avoid this, we have to hold the form data till the successful submission. For this, we just need a little PHP code in our html form, see below:
<body>
<table align='center' border='0'>
<caption><h2>Registration Form</h2></caption>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<tr><td>FullName</td><td><input type="text" name="fname"value="<?php echo $fname;?>"/> <tt class="error"><?php echo $fnameer;?></tt></td></tr>
<tr><td>Gender:
</td><td>Female<input type="radio" value="female" name="gender"<?php if(isset($gender)&&$gender=="female")echo "checked";?>/>
Male<input type="radio" value="male" name="gender"<?php if(isset($gender)&&$gender=="male")echo "checked";?>/>
<tt class="error"><?php echo $gener;?></tt></td></tr
<tr><td>Email</td><td><input type="text" name="mail"value="<?php echo $mail;?>"/><tt class="error"><?php echo $mailerr;?></tt></td></tr>
<tr><td>Password</td><td><input type="password" name="pswd"/><tt class="error"><?php echo $pswder;?></tt></td></tr>
<tr><td>Terms & condition</td><td><input type="checkbox" name="tc" value="agree"/>
<tt class="error"><?php echo $tcer?></tt></td></tr>
<tr colspan='1'><td></td><td><input type="submit" name="btnsubmit"value="SUBMIT"/>
<input type="Reset" name="btnreset" value="Reset"/></td></tr></form></table>
</body>
</html>
In the code you can see using of the value attribute of <input> tag. Check the first <input> tag. Normally the value attribute is used to display defualt text in form text-box. Here, we used that property of value attribute to hold the data. Consider the FullName field, the value="" attribute of form has a PHP, which contain the PHP varaible, which hold the user entered value, ie;
<tr><td>FullName</td><td><input type="text" name="fname"value="<?php echo $fname;?>"/> <tt class="error"><?php echo $fnameer;?></tt></td></tr>.
The statement, value="<?php echo $fname;?>, has a PHP variable $fnamewhich we just used to hold the fullname entered by the user, in form validation procces. Similliarly, use the checked attribute for radio button and checkbox .
<tr><td>FullName</td><td><input type="text" name="fname"value="<?php echo $fname;?>"/> <tt class="error"><?php echo $fnameer;?></tt></td></tr>.
The statement, value="<?php echo $fname;?>, has a PHP variable $fnamewhich we just used to hold the fullname entered by the user, in form validation procces. Similliarly, use the checked attribute for radio button and checkbox .
No comments:
Post a Comment