MySQL Insert

The INSERT statement is used to insert new records in a table.

Syntax:

It is possible to write the INSERT statement in two forms.

The first form doesn’t specify the column names where the data will be inserted, only their values:

INSERT INTO table_name VALUES (value1, value2, value3,…valueN)

The second form specifies both the field names and the values to be inserted:

INSERT INTO table_name ( field1, field2,…fieldN )VALUES( value1, value2,…valueN );

In below example we will insert id, name and age in employee table using INSERT query.

PHP & MySQL Code:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "dbusername", "dbpassword") or die(mysql_error());
mysql_select_db("test_db") or die(mysql_error());

// Get all the data from the "example" table
$insert_q = "INSERT INTO employee(id,name,age) VALUES(1,'learnwebdevelopment',25)";
$insert_r = mysql_query($insert_q);

if($insert_r)
{
echo "Data Inserted successfully";
}
else
{
die('Insert Error – '.mysql_error());
}
?>

Posted in MySQL