Insert Multiple Records into MySQL with a Single Query

Table Name tbl_student_record

ID Name Class Subject

Object-Oriented

<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here

// In this line Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// In this line  Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
// In this line Insert Multiple Records into MySQL with a single query
$sql = "INSERT INTO tbl_student_record (name, class, subject)
VALUES ('Mohan', '2nd', 'Scince');";
$sql .= "INSERT INTO tbl_student (name, class, subect)
VALUES ('Suraj Kumar', '3rd', 'Art');";
$sql .= "INSERT INTO tbl_student (name, class, subect)
VALUES ('Rohan Gupta', '4th', 'Scince')";
if ($conn->multi_query($sql) === TRUE) {
    echo "Record has been inserted";
} else {
    echo "Record has not been inserted Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?> 

You can also use this code in Object-Oriented

<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here

// In this line Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// In this line  Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
// In this line Insert Multiple Records into MySQL with a single query
$sql = "INSERT INTO tbl_student_record (name, class, subject) VALUES ('Mohan', '2nd', 'Scince'),('Suraj', '3rd', 'Art'),('Rohan Gupta', '4th', 'Scince')";
if ($conn->query($sql) === TRUE) {
    echo "Record has been inserted";
} else {
    echo "Record has not been inserted Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?> 

Procedural

<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here

// In this line Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// In this line  Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
// In this line Insert Multiple Records into MySQL with a single query
$sql = "INSERT INTO tbl_student_record (name, class, subject)
VALUES ('Mohan', '2nd', 'Scince');";
$sql .= "INSERT INTO tbl_student (name, class, subect)
VALUES ('Suraj Kumar', '3rd', 'Art');";
$sql .= "INSERT INTO tbl_student (name, class, subect)
VALUES ('Rohan Gupta', '4th', 'Scince')";

if (mysqli_multi_query($conn, $sql)) {
    echo "Record has been inserted";
} else {
    echo "Record has not been inserted Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?> 

You can also use this code in Procedural

<?php
$servername = "localhost";
$username = "username"; // when you work in your computer then use username name "root"
$password = "password"; // when you work in your computer then use password Empty
$dbname = "yourdatabase";//Type Your Database Name Here

// In this line Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// In this line  Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// In this line Insert Multiple Records into MySQL with a single query
$sql = "INSERT INTO tbl_student_record (name, class, subject) VALUES ('Mohan', '2nd', 'Scince'),('Suraj', '3rd', 'Art'),('Rohan Gupta', '4th', 'Scince')";
if (mysqli_query($conn, $sql)) {
    echo "Record has been inserted";
} else {
    echo "Record has not been inserted Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?> 

Output

 

ID Name Class Subject
1 Mohan 2nd Math
2 Suraj Kumar 3rd Scince
3 Rohan Gupta 4th Scince