Insert Multiple records in MySQLi database table – onlinecode

Insert Multiple records in MySQLi database table – onlinecode

In this post we will give you information about Insert Multiple records in MySQLi database table – onlinecode. Hear we will give you detail about Insert Multiple records in MySQLi database table – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In my previous post, you know how to insert single record in database table using MySQLi preapred statements.
Now i am going to tell you how to insert multiple record at once in MySQLi database tables.

To insert multiple records at once in MySQLi database table, you will have to include multiple lists of column values enclosed within parentheses and must be separated by (,) commas.

You can also know how many rows are affected by using mysqli_affected_rows.

Please have a look on given example :

  1. <!--?php
  2. if(!$db->query("INSERT INTO products(id) VALUES (1), (2), (3), (4)")){
  3. echo"Error: (".$db->errno .") ".$db->error;
  4. }else{
  5.     print'Successfully inserted and Total '.$db->affected_rows .' rows added.';
  6. }
  7. ?>
query("INSERT INTO products(id) VALUES (1), (2), (3), (4)")) {
    echo "Error: (" . $db->errno . ") " . $db->error;
}else{
	print 'Successfully inserted and Total ' .$db->affected_rows .' rows added.';
}
?>


Example 1 :

  1. //product 1
  2. $product_name1='"'.$db->real_escape_string('onlinecode').'"';
  3. $product_price1='"'.$db->real_escape_string('60000').'"';
  4. $product_details1='"'.$db->real_escape_string('Provide Online Tutorials').'"';
  5. //product 2
  6. $product_name2='"'.$db->real_escape_string('Demo').'"';
  7. $product_price2='"'.$db->real_escape_string('60000').'"';
  8. $product_details2='"'.$db->real_escape_string('Demo code').'"';
  9. //Insert multiple rows
  10. $insert=$db->query("INSERT INTO products(product_name, product_price, product_details) VALUES
  11. ($product_name1, $product_price1, $product_details1),
  12. ($product_name2, $product_price2, $product_details2)");
  13. if($insert){
  14. //know total inserted records using mysqli_affected_rows
  15. print'Successfully inserted and Total '.$db->affected_rows .' rows added.';
  16. }else{
  17. echo"Error: (".$db->errno .") ".$db->error;
  18. }
//product 1
$product_name1 = '"'.$db->real_escape_string('onlinecode').'"';
$product_price1 = '"'.$db->real_escape_string('60000').'"';
$product_details1 = '"'.$db->real_escape_string('Provide Online Tutorials').'"';

//product 2
$product_name2 = '"'.$db->real_escape_string('Demo').'"';
$product_price2 = '"'.$db->real_escape_string('60000').'"';
$product_details2 = '"'.$db->real_escape_string('Demo code').'"';


//Insert multiple rows
$insert = $db->query("INSERT INTO products(product_name, product_price, product_details) VALUES
($product_name1, $product_price1, $product_details1),
($product_name2, $product_price2, $product_details2)");

if($insert){
    //know total inserted records using mysqli_affected_rows
    print 'Successfully inserted and Total ' .$db->affected_rows .' rows added.'; 
}else{
    echo "Error: (" . $db->errno . ") " . $db->error;
}


Example 2 :

  1. $sql="INSERT INTO products (product_name, product_details)
  2. VALUES ('onlinecode', 'Online PHP Tutorials');";
  3. $sql.="INSERT INTO products (product_name, product_details)
  4. VALUES ('Demo','Demo code');";
  5. if($db->multi_query($sql)=== TRUE){
  6. echo"records inserted successfully";
  7. }else{
  8. echo"Error: ".$sql."
    "
    .$db->error;
  9. }
  10. $db->close();
$sql = "INSERT INTO products (product_name, product_details)
VALUES ('onlinecode', 'Online PHP Tutorials');";
$sql .= "INSERT INTO products (product_name, product_details)
VALUES ('Demo','Demo code');";

if ($db->multi_query($sql) === TRUE) {
    echo "records inserted successfully";
} else {
    echo "Error: " . $sql . "
" . $db->error; } $db->close();
mysqli_multi_query function is used to execute multiple SQL statements.

Hope this code and post will helped you for implement Insert Multiple records in MySQLi database table – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  35  =  37

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US