简体   繁体   中英

How can I insert multiple rows into database with single submit

Can anyone here help me on how can I use PHP to insert multiple rows into a database with a single submit? I have tried doing it, but it only inserts one row.

Here is my code:

<?php
if(isset($_POST['insertData']))
{
   $pred1 =$_POST['pre']; 
    $np1 =$_POST['nap']; 
    $sd101 =$_POST['tdisease'];
    $pr1 =$_POST['pric1'];  
    $ivd =$_POST['invd'];
    $id =$_POST['user'];

     $pred1 =$_POST['pre1']; 
    $np1 =$_POST['nap1']; 
    $sd101 =$_POST['tdisease1'];
    $pr1 =$_POST['pric1'];

     $pred2 =$_POST['pre2']; 
    $np2 =$_POST['nap2']; 
    $sd102 =$_POST['tdisease2'];
    $pr2 =$_POST['pric2'];



      $insert_user="INSERT INTO invoices(id, icd10, nappi_code, prescription, price, invoice_date, pid) VALUES ('','$sd10', '$np' ,'$pred','$pr','$ivd','$id');";
       $insert_user .="INSERT INTO invoices(id, icd10, nappi_code, prescription, price, invoice_date, pid) VALUES ('','$sd101', '$np1' ,'$pred1','$pr1','$ivd','$id');";
        $insert_user .="INSERT INTO invoices(id, icd10, nappi_code, prescription, price, invoice_date, pid) VALUES ('','$sd102', '$np2' ,'$pred2','$pr2','$ivd','$id')";


    if(mysqli_query($con,$insert_user))
    {  
        echo"<script>alert(' Invoice Details successfuly added to database')</script>";
        echo '<meta content="1;generate-invoive-results-date-report.php?id='.$id.'" http-equiv="refresh" />';// redirects user view page after 3    
    }else{  
        echo"<script>alert('Unknown error occured')</script>";   
  } 
}
?>

My guess is that the first insert completes, and then the subsequent two are being ignored. But why don't you insert all data in a single statement?

$insert_user="INSERT INTO invoices(id, icd10, nappi_code, prescription, price, invoice_date, pid) VALUES ('','$sd10', '$np' ,'$pred','$pr','$ivd','$id'),";
$insert_user .="('','$sd101', '$np1','$pred1','$pr1','$ivd','$id'),";
$insert_user .="('','$sd102', '$np2' ,'$pred2','$pr2','$ivd','$id');";

You should try this perhaps:

$insert_user="INSERT INTO invoices(id, icd10, nappi_code, prescription, price, invoice_date, pid) VALUES ('','$sd10', '$np' ,'$pred','$pr','$ivd','$id'),('','$sd101', '$np1' ,'$pred1','$pr1','$ivd','$id'), ('','$sd102', '$np2' ,'$pred2','$pr2','$ivd','$id')";

And also, This:

$pred1 =$_POST['pre']; 
$np1 =$_POST['nap']; 
$sd101 =$_POST['tdisease'];
$pr1 =$_POST['pric1'];  
$ivd =$_POST['invd'];
$id =$_POST['user'];

$pred1 =$_POST['pre1']; 
$np1 =$_POST['nap1']; 
$sd101 =$_POST['tdisease1'];
$pr1 =$_POST['pric1'];

$pred2 =$_POST['pre2']; 
$np2 =$_POST['nap2']; 
$sd102 =$_POST['tdisease2'];
$pr2 =$_POST['pric2'];

Should've been:

$ivd =$_POST['invd'];
$id =$_POST['user'];

$pred =$_POST['pre']; 
$np =$_POST['nap']; 
$sd10 =$_POST['tdisease']; <== 
$pr =$_POST['pric1'];  


$pred1 =$_POST['pre1']; 
$np1 =$_POST['nap1']; 
$sd101 =$_POST['tdisease1'];
$pr1 =$_POST['pric1'];

$pred2 =$_POST['pre2']; 
$np2 =$_POST['nap2']; 
$sd102 =$_POST['tdisease2'];
$pr2 =$_POST['pric2'];

Because the first two sets have the same variable names and they'll be overwritten.

====================== Edit ===========================

The reason why it didn't work for you before was probably because:

1) The variables '$sd10', '$np' ,'$pred','$pr' didn't exist in the 1st query:

$insert_user="INSERT INTO invoices(id, icd10, nappi_code, prescription, price, invoice_date, pid) VALUES ('','$sd10', '$np' ,'$pred','$pr','$ivd','$id');";

2) There was no ; at the end of the 3rd query:

VALUES ('','$sd102', '$np2' ,'$pred2','$pr2','$ivd','$id')<no-semi-colon-here>";

As they are being inserted as 3 separate queries.

A query need not be given all on a single line, so lengthy queries that require several lines are not a problem. mysql determines where your statement ends by looking for the terminating semicolon, not by looking for the end of the input line. (In other words, mysql accepts free-format input: it collects input lines but does not execute them until it sees the semicolon.)

Refer: https://dev.mysql.com/doc/refman/5.7/en/entering-queries.html

You should have a look at the function mysqli_multi_query . See example here : https://www.w3schools.com/php/php_mysql_insert_multiple.asp

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM