How To Implement Insert Query In PHP?

Last updated on Jun 20,2023 15.8K Views

How To Implement Insert Query In PHP?

edureka.co

Before with start with this Insert Query in PHP article, you must be aware of how to create a database in MySQL. So without any further delay let us get started with this article,

In order to insert new rows in a database table, we use INSERT INTO statement in PHP. We execute the insert query in PHP by passing mysqli_query(). If you want to execute more than one query in a single call, we use mysqli_multi_query as mysqli_query will not execute multiple queries to prevent SQL injections. Please take note of following syntax before we move any further,

Syntax for insert into a table: INSERT INTO TABLE_NAME (column1, column2,… columnN) VALUES (value1, value2,…valueN);

Syntax for mysqli_query(): mysqli_query(connection,query,resultmode);

Syntax for mysqli_multi_query(): mysqli_multi_query(connection,query);

Now that basics are out of the way let us take a look at the pointers to be covered in this Insert Query in PHP,

So let us get started then,

How to insert data using MySQLi Object-oriented Procedure?

<?php
$server= "localhost";
$user = "root";
$password = "";
$db = "feedback";
// Create connection
$conn = new mysqli($server, $user, $password, $db);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql =INSERT INTO data VALUES(&lsquo;ashok','you are awesome bro')";
if ($conn->query($sql) === TRUE)
{
echo "feedback sucessfully submitted";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Moving on with Insert Query in PHP article,

How to insert data using MySQLi Procedural Procedure?

Moving on with Insert Query in PHP article,

<?php
$server= "localhost";
$user = "root";
$password = "";
$db = "feedback";
// Create connection
$conn = mysqli_connect($server, $user, $password, $db);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql =INSERT INTO data VALUES(&lsquo;ashok','you are awesome bro')";
if (mysqli_query($conn, $sql))
{
echo "feedback sucessufully submitted";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>

How to insert multiple records using MySQLi Object-oriented Procedure?

<?php
$server= "localhost";
$user = "root";
$password = "";
$db = "feedback";
// Create connection
$conn = new mysqli($server, $user, $password, $db);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql =INSERT INTO data VALUES(&lsquo;ashok','you are awesome')";
$sql =INSERT INTO data VALUES(&lsquo;sushma','you are mad')";
$sql =INSERT INTO data VALUES(&lsquo;charan','you are an idiot')";
$sql =INSERT INTO data VALUES(&lsquo;tarun','you are scanner')";
if ($conn->multi_query($sql) === TRUE)
{
echo "feedbacks successfully submitted";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Find out our Flutter Course in Top Cities

IndiaOther Countries
Flutter Training in Chennai Flutter Course in Australia
Flutter Course in BangaloreFlutter Course in Canada
Flutter Training in HyderabadFlutter Course in London

Moving on with Insert Query in PHP article,

How to insert multiple records using MySQLi Procedural Procedure?

<?php
$server= "localhost";
$user = "root";
$password = "";
$db = "feedback";
// Create connection
$conn = mysqli_connect($server, $user, $password, $db);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql =INSERT INTO data VALUES(&lsquo;ashok','you are awesome')";
$sql =INSERT INTO data VALUES(&lsquo;sushma','you are mad')";
$sql =INSERT INTO data VALUES(&lsquo;charan','you are an idiot')";
$sql =INSERT INTO data VALUES(&lsquo;tarun','you are scanner')";
if (mysqli_multi_query($conn, $sql))
{
echo "feedbacks succesfully submitted";
} else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>

With this we come to an end of this article, I hope you have learned about how to insert data into MYSQL using MySQLi Object-oriented & Procedural Procedure and also how to insert multiple records into MySQL using MySQLi Object-oriented & Procedural Procedure.

With this we come to an end of this article on Insert Query In PHP, I hope you have learned about  how to create a Database and store the data obtained from the form which is entered by a user and fetching the user data from the database.

If you found this Data Retrieval In PHP article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of  this article and I will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework