Insert Data in MySQL using PHP | Insertion In PHP
In Today’s tutorial we will see how to insert data in MySQL using PHP, But for that we require database and table to be created.
In PHP Tutorial, we have seen one session on “How to Create Database and Table in MySQL using PHP”. We have database already created named “Employee” and table named “Emp” with following fields.
Field Name | DataType | Description |
---|---|---|
empId | int | Employee Id, Not NULL, PRIMARY KEY , AUTO INCREMENT |
firstName | varchar(30) | Employee First Name |
lastName | varchar(30) | Employee Last Name |
age | int | Employee Age |
designation | varchar(30) | Employee Designation |
salary | int | Employee Salary |
First we will see HTML part of the code :
<html> <body> <h2 align="center"><b>Employee Data Insertion</b></h2> <form method="post" action="emp_insert.php"> <center> <table border="1" cellspacing="20"> <tr> <td> Enter First Name : </td> <td><input type="text" name="txtFname" size="30"></td> </tr> <tr> <td> Enter Last Name : </td> <td><input type="text" name="txtLname" size="30"></td> </tr> <tr> <td> Enter Age : </td> <td><input type="text" name="txtage" size="30"></td> </tr> <tr> <td> Enter Designation : </td> <td><input type="text" name="txtdes" size="30"></td> </tr> <tr> <td> Enter Salary : </td> <td><input type="text" name="txtsalary" size="30"></td> </tr> <tr><td colspan="2" align="center"><Input type="submit" value="Insert" name="Insert"></td></tr> </table> </form> </center> </body> </html>
OUTPUT :
Once done with HTML part, we will move to PHP code i.e. we will establish connection, select database and then insert data into table. Lets see the PHPcode :
<?php if(isset ($_REQUEST["Insert"])) // This will run when the code will be submitted. { $fname=$_REQUEST["txtFname"]; $lname=$_REQUEST["txtLname"]; $age=$_REQUEST["txtage"]; $des=$_REQUEST["txtdes"]; $salary=$_REQUEST["txtsalary"]; $con=mysql_connect("localhost","root",""); if(!$con) { echo "Could Not Connect".mysql_error(); } else { //echo "Connected Successfully.<br>"; $result=mysql_select_db("employee",$con); if(!$result) { echo "Could Not Select database ".mysql_error(); } else { $qry="insert into emp (firstName, lastName, age, designation,salary) values('".$fname."','".$lname."','".$age."','".$des."','".$salary."')"; $res=mysql_query($qry); if(!$res) { echo "Insertion Error ".mysql_error(); } else { echo "<br>1 Record Inserted Sucessfully"; } } } } ?>
OUTPUT :
In this PHP code we have used different function and that are:
- Here we have not inputted empId as it is auto_increment field.
- $_REQUEST super global variable and helps the variable to be accessible thoroughout the application.
- mysql_connect() : Used to open connection in MySQL and takes three parameter i.e. SERVER,USER_NAME and PASSWORD. we take default values for these parameters i.e. localhost, root and empty string respectively. This function returns a MySQL link identifier on successful connection and return false if fails to open connection.
- mysql_error() : this returns the text of the error message from previous MySQL operation and empty string if no error occurred.
- mysql_select_db() : Sets the current active database on the server which is associated with specified link identifier returned by mysql_connect().
- mysql_query() : This sends a query to current active database on the server. It takes SQL query as parameter.
Lets see complete code of insert data in MySQL using PHP :
<html> <body> <h2 align="center"><b>Employee Data Insertion</b></h2> <form method="post" action="emp_insert.php"> <center> <table border="1" cellspacing="20"> <tr> <td> Enter First Name : </td> <td><input type="text" name="txtFname" size="30"></td> </tr> <tr> <td> Enter Last Name : </td> <td><input type="text" name="txtLname" size="30"></td> </tr> <tr> <td> Enter Age : </td> <td><input type="text" name="txtage" size="30"></td> </tr> <tr> <td> Enter Designation : </td> <td><input type="text" name="txtdes" size="30"></td> </tr> <tr> <td> Enter Salary : </td> <td><input type="text" name="txtsalary" size="30"></td> </tr> <tr><td colspan="2" align="center"><Input type="submit" value="Insert" name="Insert"></td></tr> </table> </form> </center> </body> </html> <?php if(isset ($_REQUEST["Insert"])) { $fname=$_REQUEST["txtFname"]; $lname=$_REQUEST["txtLname"]; $age=$_REQUEST["txtage"]; $des=$_REQUEST["txtdes"]; $salary=$_REQUEST["txtsalary"]; $con=mysql_connect("localhost","root",""); if(!$con) { echo "Could Not Connect".mysql_error(); } else { //echo "Connected Successfully.<br>"; $result=mysql_select_db("employee",$con); if(!$result) { echo "Could Not Select database ".mysql_error(); } else { $qry="insert into emp (firstName, lastName, age, designation,salary) values('".$fname."','".$lname."','".$age."','".$des."','".$salary."')"; $res=mysql_query($qry); if(!$res) { echo "Insertion Error ".mysql_error(); } else { echo "<br>1 Record Inserted Sucessfully"; } } } } ?>
This is the way, you can insert data in MySQL using PHP. In this tutorial we have not applied any validation to HTML form. You can Restrict user data and get guidance from earlier tutorial i.e. how to keep validations in the form .