[英]Inserting array values and other from values of a form into two different mysql tables
I'am trying to insert data from a form into two different tables. 我正在尝试将表单中的数据插入两个不同的表中。 Here's what I'am doing:-
这是我在做什么:-
<?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";
}
?>
Now, data from the first query is getting stored into bill_details table. 现在,来自第一个查询的数据将存储到bill_details表中。 but the array values are not getting stored.
但是不会存储数组值。 I cant figure out what am I doing wrong with my code.
我无法弄清楚我的代码在做什么。 I wanna know how can i solve this problem and use the invoice_no as reference key for both the tables.
我想知道如何解决此问题,并使用invoice_no作为两个表的参考键。
Here are the structure of both the database tables.. 这是两个数据库表的结构。
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";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.