doukengzi3517 2016-11-06 14:31
浏览 34

将数组值和其他值从表单的值插入到两个不同的mysql表中

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. 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.

Here are the structure of both the database tables..

bill_details table

bill_records table

  • 写回答

1条回答 默认 最新

  • dongwolu5275 2016-11-06 14:44
    关注
    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";
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程