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