[英]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? 这里有人可以帮助我如何使用
PHP
通过一次提交将多行插入数据库吗? 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: 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) There was no ;
2)没有
;
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. 由于将它们作为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.
mysql通过查找终止分号(而不是通过输入行的末尾)来确定语句的结尾。 (In other words, mysql accepts free-format input: it collects input lines but does not execute them until it sees the semicolon.)
(换句话说,mysql接受自由格式的输入:它收集输入行,但直到看到分号后才执行它们。)
Refer: https://dev.mysql.com/doc/refman/5.7/en/entering-queries.html 参考: https : //dev.mysql.com/doc/refman/5.7/en/entering-queries.html
You should have a look at the function mysqli_multi_query . 您应该看看函数mysqli_multi_query 。 See example here : https://www.w3schools.com/php/php_mysql_insert_multiple.asp
在此处查看示例: https : //www.w3schools.com/php/php_mysql_insert_multiple.asp
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.