Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

First Create a database with name demo.

Now create a table with


name data.
Here the strcuture of table
1 CREATE TABLE IF NOT EXISTS `data` (
2 `id` int(11) NOT NULL,
3 `name` varchar(255) NOT NULL,
4 `email` varchar(255) NOT NULL,
5 `contactno` bigint(20) NOT NULL,
6 `gender` varchar(255) NOT NULL,
7 `education` varchar(255) NOT NULL,
8 `address` longtext NOT NULL
9 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
Code for mysql connection with php
1 <?php
2 define('DB_SERVER','localhost');
3 define('DB_USER','root');
4 define('DB_PASS' ,'');
5 define('DB_NAME', 'demos');
6 $conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS)
7 or
8 die('localhost connection problem'.mysql_error());
9 mysql_select_db(DB_NAME, $conn);
10 ?>
Create a HTML Form for user inputs
1 <form name="insert" action="" method="post">
2 <table width="100%" border="0">
3 <tr>
4 <th height="62" scope="row">Name </th>
5 <td width="71%"><input type="text" name="name" id="name" value="" class="form-control" required />
6 </td>
7 </tr>
8 <tr>
9 <th height="62" scope="row">Email id </th>
10 <td width="71%"><input type="email" name="email" id="email" value="" class="form-control"
11 required />
12 </td>
13 </tr>
14 <tr>
15 <th height="62" scope="row">Contact no</th>
16 <td width="71%">
17 <input type="text" name="contactno" id="contactno" value="" class="form-control" maxlength="10"
18 required /></td>
19 </tr>
20 <tr>
21 <th height="62" scope="row">Gender</th>
22 <td width="71%"><input type="radio" name="gender" value="Male" checked >Male
23 <input type="radio" name="gender" value="Female" >female</td>
24 </tr>
25 <tr>
26 <th height="62" scope="row">Education</th>
<td width="71%"><select name="education" id="email" class="form-control" required >
27 <option value="">Select your Education</option>
28 <option value="Post Graduate">Post Graduate</option>
29 <option value="Graduate">Graduate</option>
30 <option value="Intermediate">Intermediate</option>
31 <option value="High School">High School</option>
32 <option value="Other">Other</option>
33 </select></td>
34 </tr>
35 <tr>
36 <th height="62" scope="row">Address</th>
37 <td width="71%"><textarea name="addrss" class="form-control" required></textarea> </td>
38 </tr>
39 <tr>
40 <th height="62" scope="row"></th>
41 <td width="71%"><input type="submit" name="submit" value="Submit" class="btn-group-sm" /> </td>
42 </tr>
43 </table>
</form>
Now get user inputs and store in the PHP variables
1 $name=$_POST['name'];
2 $email=$_POST['email'];
3 $contactno=$_POST['contactno'];
4 $gender=$_POST['gender'];
5 $education=$_POST['education'];
6 $adress=$_POST['addrss'];
Query for insert data into mysql using PHP
1 $query=mysqli_query($conn,"insert into data(name,email,contactno,gender,education,address)
2 values('$name','$email','$contactno','$gender','$education','$adress')");
Full PHP Script for insert data into database using mysql
1 if(isset($_POST['submit']))
2 {
3 $name=$_POST['name'];
4 $email=$_POST['email'];
5 $contactno=$_POST['contactno'];
6 $gender=$_POST['gender'];
7 $education=$_POST['education'];
8 $adress=$_POST['addrss'];
9 $query=mysqli_query($conn,"insert into data(name,email,contactno,gender,education,address)
10 values('$name','$email','$contactno','$gender','$education','$adress')");
11 if($query)
12 {
13 echo "<script>alert('Data inserted successfully');</script>";
14 }
15 else
16 {
17 echo "<script>alert('Data not inserted');</script>";
18 }
19 }
Here is the full code that we have written during this tutorial:
1 <?php
2 include_once("config.php");
3 if(isset($_POST['submit']))
4 {
5 $name=$_POST['name'];
6 $email=$_POST['email'];
7 $contactno=$_POST['contactno'];
8 $gender=$_POST['gender'];
9 $education=$_POST['education'];
10 $adress=$_POST['addrss'];
11 $query=mysql_query("insert into data(name,email,contactno,gender,education,address)
12 values('$name','$email','$contactno','$gender','$education','$adress')");
13 if($query)
14 {
15 echo "<script>alert('Data inserted successfully');</script>";
16 }
17 else
18 {
19 echo "<script>alert('Data not inserted');</script>";
20 }
21 }
22 ?>
23 <!DOCTYPE html>
24 <html lang="en">
25 <head>
26 </head>
27 <body>
28 <form name="insert" action="" method="post">
29 <table width="100%" border="0">
30 <tr>
31 <th height="62" scope="row">Name </th>
32 <td width="71%"><input type="text" name="name" id="name" value="" class="form-control" required />
33 </td>
34 </tr>
35 <tr>
36 <th height="62" scope="row">Email id </th>
37 <td width="71%"><input type="email" name="email" id="email" value="" class="form-control"
38 required />
39 </td>
40 </tr>
41 <tr>
42 <th height="62" scope="row">Contact no</th>
43 <td width="71%">
44 <input type="text" name="contactno" id="contactno" value="" class="form-control" maxlength="10"
45 required /></td>
46 </tr>
47 <tr>
48 <th height="62" scope="row">Gender</th>
49 <td width="71%"><input type="radio" name="gender" value="Male" checked >Male
50 <input type="radio" name="gender" value="Female" >female</td>
51 </tr>
52 <tr>
<th height="62" scope="row">Education</th>
53 <td width="71%"><select name="education" id="email" class="form-control" required >
54 <option value="">Select your Education</option>
55 <option value="Post Graduate">Post Graduate</option>
56 <option value="Graduate">Graduate</option>
57 <option value="Intermediate">Intermediate</option>
58 <option value="High School">High School</option>
59 <option value="Other">Other</option>
60 </select></td>
61 </tr>
62 <tr>
63 <th height="62" scope="row">Address</th>
64 <td width="71%"><textarea name="addrss" class="form-control" required></textarea> </td>
65 </tr>
66 <tr>
67 <th height="62" scope="row"></th>
68 <td width="71%"><input type="submit" name="submit" value="Submit" class="btn-group-sm" /> </td>
69 </tr>
70 </table>
71 </form>
72 </body>
</html>

You might also like