Insert data in mysql using php
We already know how to establish connection between PHP web application and MySQL database system in our previous PHP tutorial. we also learnt to select data from mysql database.
Now, In this PHP tutorial we will learn how to insert data into MySQL database using PHP script.
INSERT Statement
The INSERT statement is used to insert record into a table in database.
INSERT Statement Syntax
INSERT INTO table_name
(Col1, Col2……ColN) VALUES (Val1, Val2……ValN);Col1, Col2 – Indicate the Columns name of table.
Val1, Val2 – is the value of columns respectively.
learn more about mysql INSERT statement : INSERT Statement
PHP Example of Insert data into database
<?php
$username = "username";
$password = "password";
$hostname = "localhost";
$database="School";
//connection to the mysql database,
$dbhandle = mysqli_connect($hostname, $username, $password,$database);
if(mysqli_query($dbhandle, "Insert into StudentMst values (4,'Ram','Surat')"))
{
echo "Record Added Successfully";
}
else
{
echo "Error!!";
}
//close the connection
mysqli_close($dbhandle);
?>