这里的任何人都可以帮助我如何使用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>";
}
}
?>
答案 0 :(得分:1)
我的猜测是第一个插入完成,然后后续的两个被忽略。但是,为什么不在一个声明中插入所有数据呢?
$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');";
答案 1 :(得分:1)
你应该尝试这个:
$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个单独的查询。
不需要在一行上提供查询,因此查询冗长 这需要几行不是问题。 mysql确定在哪里 你的陈述以寻找终止分号结束,而不是通过 寻找输入行的结尾。 (换句话说,mysql接受 自由格式输入:它收集输入行但不执行它们 直到它看到分号。)
参考:https://dev.mysql.com/doc/refman/5.7/en/entering-queries.html
答案 2 :(得分:0)
你应该看一下 mysqli_multi_query 这个函数。请参阅此处的示例:https://www.w3schools.com/php/php_mysql_insert_multiple.asp
希望这有帮助