这里的任何人都可以帮助我如何使用 PHP
通过一次提交将多行插入到数据库中?我试过这样做,但它只插入一行。
这是我的代码:
<?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>";
}
}
?>
也许你应该试试这个:
$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')";
还有,这个:
$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'];
应该是:
$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'];
因为前两组变量名相同,会被覆盖。
======================编辑======================== ===
之前它对你不起作用的原因可能是因为:
1) 第一个查询中不存在变量 '$sd10', '$np' ,'$pred','$pr'
:
$insert_user="INSERT INTO invoices(id, icd10, nappi_code, prescription, price, invoice_date, pid) VALUES ('','$sd10', '$np' ,'$pred','$pr','$ivd','$id');";
2) 第三个查询末尾没有;
:
VALUES ('','$sd102', '$np2' ,'$pred2','$pr2','$ivd','$id')<no-semi-colon-here>";
因为它们作为 3 个单独的查询插入。
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.)
引用:https://dev.mysql.com/doc/refman/5.7/en/entering-queries.html