我正在尝试将表单中的数据插入到两个不同的表中。这就是我在做的事情: -
<?php
$mysqli= new mysqli("localhost","root","","store_records");
if($mysqli->connect_error)
die("Database connection failed ".$mysqli->connect_error);
$query = "insert into bill_details(date,invoice_no,balance) values('".$_POST['p_date']."','".$_POST['invoice_no']."','".$_POST['balance']."')";
if($mysqli->query($query))
{
$cquery="";
for ( $i=0;$i<$_POST['row_numbers'];$i++)
{
$cquery .= "insert into bill_records(item_name,qty,pack,batch,expiry,mrp,rate,vat,discount,invoice_no) values('".$_POST['item_name'][$i]."','".$_POST['qty'][$i]."','".$_POST['pack'][$i]."','".$_POST['batch'][$i]."','".$_POST['expiry'][$i]."','".$_POST['mrp'][$i]."','".$_POST['rate'][$i]."','".$_POST['vat'][$i]."','".$_POST['discount'][$i]."','".$_POST['invoice_no']."');";
}
if($mysqli->multi_query($cquery))
echo "Records Saved";
else
echo "Failed to save product records";
}
else
{
echo "Failed To save Records";
}
?>
现在,来自第一个查询的数据将存储到 bill_details 表中。但是没有存储数组值。我无法弄清楚我的代码出了什么问题。我想知道如何解决这个问题,并使用 invoice_no 作为两个表的参考密钥。
以下是两个数据库表的结构..
答案 0 :(得分:0)
Try this. Hope it works. :)
<?php
$mysqli= new mysqli("localhost","root","","store_records");
if($mysqli->connect_error)
die("Database connection failed ".$mysqli->connect_error);
$query = "insert into bill_details(date,invoice_no,balance) values('".$_POST['p_date']."','".$_POST['invoice_no']."','".$_POST['balance']."')";
if($mysqli->query($query))
{
$cquery="";
for ( $i=0;$i<$_POST['row_numbers'];$i++)
{
$cquery .= "insert into bill_records(item_name,qty,pack,batch,expiry,mrp,rate,vat,discount,invoice no) values('".$_POST['item_name'][$i]."','".$_POST['qty'][$i]."','".$_POST['pack'][$i]."','".$_POST['batch'][$i]."','".$_POST['expiry'][$i]."','".$_POST['mrp'][$i]."','".$_POST['rate'][$i]."','".$_POST['vat'][$i]."','".$_POST['discount'][$i]."','".$_POST['invoice_no']."');";
if(!($mysqli->query($cquery)))
die("failed to save");
}
}
else{
echo "Failed To save Records";
}
?>