PHP store form Data into database
upload form data into database
You can store form data by using different super global array like $_GET, $_POST and $_REQUEST, but in this case $_POST is used for store the form data into the database.
Php, css and html code are below in different section and complete script can be downloaded from clicking the download button below shown.
Create form using html 5
<!doctype html> <html> <head> </head> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <table > <tr> <td>Id:<br><input type="text" name="roll" value="<?php if(isset($sub)){echo $roll;} ?>" placeholder="pT-14520"> </tr> <tr> <td>First Name:<input type="text" name="name" maxlength="20" placeholder="First Name" ></td> </tr> <tr> <td>Last Name:<input type="text" name="lname" maxlength="20" placeholder="Last Name"></td> </tr> <tr> <td>Date of Birth:<input type="Date" name="dof" maxlength="20" placeholder="28/12/1993"></td> </tr> <tr> <td>Qualification:<input type="text" name="qua" maxlength="20" placeholder="Qualification"></td> </tr> <tr> <td>Sex:<input type="radio" name="sex" value="male" checked="checked">male <input type="radio" name="sex" value="female">female </td> </tr> <tr> <td>Email:<input type="email" name="email" autocomplete="on" ></td> </tr> <tr> <td>Contact:<input type="text" name="contact" maxlength="10" placeholder="1234567890"></td> </tr> <tr> <td>Address:<textarea rows="7" cols"3" name="a"></textarea></td> </tr> <tr> <td><input type="submit" value="PoSt" name="sub"><input type="reset" value="reset"></td> </tr> </table> </form> </body> </html>
This is a simple form using HTML 5 and nothing else you can create form your own way, it's not a big deal.
CSS code for Formatting
<style> table { background: #EDEDED; border:2px solid ; width:200px; border-spacing:10px; margin-left:auto; margin-right:auto; } </style>
Small amount of CSS for formatting the form, you can design form as you want.
PHP script for store data on database
<?php $a1=mysql_connect("localhost", "root", "")or die("connection fail"); $b=mysql_select_db("test", $a1); if(isset($_POST['sub'])) { error_reporting(0); $roll=$_POST['roll']; $name=$_POST['name']; $lname=$_POST['lname']; $dob=$_POST['dof']; $age=$_POST['age']; $qua=$_POST['qua']; $sex=$_POST['sex']; $email=$_POST['email']; $contact=$_POST['contact']; $a=$_POST['a']; $hh="INSERT INTO reg_form (roll,name,lname,dof,qua,sex,email,contact,a) VALUES ('$roll','$name','$lname','$dob','$qua','$sex','$email','$contact','$a')"; $query=mysql_query($hh); if($query) { $bg="Data sent successfully"; } else { echo"fail"; } } ?> <?php if(isset($_POST['sub'])) { echo "" ."".$bg.""."
"; header("Refresh: 3; url=http://www.ptutorial.com"); echo "You will be redirected to ptutorial in 3 seconds..."; } ?>
Output
Explanation
Fist combine all three code into a single dot php file and then create database and table as above shown in the code. If you do not know how to create database and table go back to the previous section PHP mysql introduction.
After that connect to the database and select database according to the line number 2 and 3 in the PHP section.
After that collect all the form data using $_POST super global array into the different variable.
After that execute mysql query with the help of mysql_query() function and store the data into the table.
Line number 39 is used to redirecting the current page to the www.pTutorial.com.
Before run this script please create database named test and import SQL file (test.sql) that are given in downloaded folder.