PHP Basics

PHP MySQLi Insert Record

In previous tutorial, we have learned how to create a table using mysqli and php. In this article we are going to learn how to insert record into table. For reference look at the below table.

emp_id emp_name emp_age
1 Raj 22
2 Rahul 24
3 Vivek 26
4 Ajay 28
5 Suraj 29

In the table, I am going to insert above five records.

Inserting record into table involves following three steps.

  1. Step 1: Create Connection
  2. Step 2: Insert Record
  3. Step 3: Close connection

From the above table reference, lets insert the first record.

Id is 1, name is Raj and age is 22.

Therefore, the sql command to insert record would be

$sql=”insert into employee_info(id,emp_name,emp_age)values(1,’raj’,22)”

Insert Record into Table


$sql=”insert into employee_info(id,emp_name,emp_age)values(1,’raj’,22)”;


echo “Inserted Successfully”;


echo “Error: “.mysqli_error($conn);


If I run the above code, then if record is inserted successfully then it will print ‘Inserted Successfully’. Other wise the code mysqli_error($conn) in the above is going to print the error.

Warning: Never use mysqli_error($conn) code in your php script if it is alive or available online. Use this line of code only if you are learning or developing a website in local server. Because this error message contains the error info along with your database table details. So it will hint hacker how to hack the table. So avoid using the printing of error messsage.