【问题标题】:Insert multiple rows in to mysql and restrict one of the rows to be entered only once在mysql中插入多行并限制其中一行只能输入一次
【发布时间】:2013-04-04 18:42:53
【问题描述】:

我有一个发票表单,它会将多行发布到插入页面以插入多行。我的绊脚石是,当我使用 foreach 语句插入多个订单项目时,我还插入了每一行的发票总额。我将如何只在我选择的列中插入的最后一行插入例如“发票总金额”。

我已经附上了我用来插入表单的代码,在提到它之前,为了清楚起见,我已经删除了对数据清理函数的调用。


<?php 
  //Lets connect to the database
   if(mysqli_connect_errno()){
       exit("Failed to connect to the database".mysqli_connect_error());}
        //We first insert the header line of the purchase invoice, then we get the insert id and switch to the details table and insert 
         //the purchased items details

       $query ="INSERT INTO acc_posting_headers (company_id,supplier_id,header_ref,header_date,header_type,posting_ref,due_date)
       values
       ('$company_id','$supplier_id','$receipt_ref','$invoice_date','$header_type','$syspost_ref','$due_date')";

        //Execute the $sql query on the server to insert the values
          if ($conn->query($query) === TRUE) {
              $last_post_header_id =  $conn->insert_id;
              $post_header_id = $last_post_header_id;

              /* We have now switched to the details table, so we got the last insert id, we have set the variable
              $post header and now insert each purchased item */

              foreach ($_POST['lst_nominal_code'] as $row=>$id){
                $nominal_code = $_POST['hidden_nominal_code'][$row];
                 $quantity  = $_POST['txt_quantity_'][$row];
                  $item_cost = $_POST['txt_item_cost'][$row];
                   $line_total = $_POST['txt_line_total'][$row];
                    $description = $_POST['txt_description'][$row];
                     $trade_creditors = 2109;
                      $invoice_gross = $_POST['txt_gross'];
                       $vat_line = $_POST['txt_line_vat_'][$row];
                        $nominal_id = $id;
                          $subtotal = $_POST['txt_subtotal'];
                           $vat_total = $_POST['txt_vat_total'];
                            $vat_control = 2200;

           $query = ("INSERT INTO acc_posting_details 
           (post_header_id,nominal_acc_no,quantity,price,line_total,line_vat,vat_total,description,subtotal,debit,credit)
            VALUES 
            ('$post_header_id','$nominal_code','$quantity','$item_cost','$line_total','$vat_line','0','$description','$subtotal','$line_total','0'),
            ('$post_header_id','$vat_control','0','0','0','$vat_total','total VAT','vat content','0','$vat_total','0'),
            ('$post_header_id','$trade_creditors','0','0','0','0','0','trade Creditors','0','0','$invoice_gross')");

             if ($conn->query($query) === TRUE) {

          Header("Location:../add_purchase_invoice.php");



           } else {
   echo 'Error: '. $conn->error;
        }
          }
           }

              $conn->close();
              ?>

                  <?php 
                   // echo "<pre>";
                   // print_r($_POST);
                   // echo "<pre>";
                   //echo "vat control".$vat_control;
                    //echo "vat total is:".$vat_total

                  ?>

解决了,一旦我仔细看了一下,通过更改插入的顺序并从“foreach”中删除,我用下面的代码实现了我想要的。感谢所有帮助。

 <?php session_start();?>
      <?php require_once('../Connections/connpay.php'); ?>
      <?php require_once('../functions/global_functions.php'); ?>
      <?php require_once('../functions/account_functions.php'); ?>

<?php 

        $supplier_id = clean_data($_POST['lst_supplier']);
        $receipt_ref = clean_data($_POST['txt_receipt_ref']);
        $invoice_date = clean_data($_POST['txt_receipt_date']);
        $due_date = clean_data ($_POST['txt_receipt_due']);
        $due_date = clean_data($_POST['txt_receipt_due']);
        $company_id = clean_data($_POST['hidden_company_id']);
        $syspost_ref = clean_data($_POST['txt_post_ref']);
        $header_type = "puchase_invoice";
        $nominal_id = clean_data($_POST['lst_nominal_code']);

    ?>

<?php 
  //Lets connect to the database
   if(mysqli_connect_errno()){
       exit("Failed to connect to the database".mysqli_connect_error());}
        //We first insert the header line of the purchase invoice, then we get the insert id and switch to the details table and insert 
         //the purchased items details

          $query ="INSERT INTO acc_posting_headers (company_id,supplier_id,header_ref,header_date,header_type,posting_ref,due_date)
          values
         ('$company_id','$supplier_id','$receipt_ref','$invoice_date','$header_type','$syspost_ref','$due_date')";

        //Execute the $sql query on the server to insert the values
          if ($conn->query($query) === TRUE) {

            // we now get the last insert id, and set it to a variable, we also declare the vat & gross amount variables
              $last_post_header_id =  $conn->insert_id;
              $post_header_id = $last_post_header_id;
              $vat_control = 2200;
              $vat_total = clean_data($_POST['txt_vat_total']);
              $trade_creditors = 2109;
              $invoice_gross = clean_data($_POST['txt_gross']);


               //We are now at the posting details table, we insert the trade creditors and total vat cr amounts first.
               $query = ("INSERT INTO acc_posting_details 
               (post_header_id, nominal_acc_no ,quantity,price,line_total,line_vat,vat_total,description,subtotal,debit,credit)
               VALUES 
               ('$post_header_id','$trade_creditors','0','0','0','0','0','trade Creditors','0','0','$invoice_gross'), 
               ('$post_header_id','$vat_control','0','0','0','$vat_total','0','vat content','0','$vat_total','0')");
                $conn->query($query);   

              // Now that we have inserted the two lines to show the trade creditors and vat CR and DR amounts we 
              //carry on and insert each invoice line from the posted array.
              foreach ($_POST['lst_nominal_code'] as $row=>$id){
              $nominal_code = $_POST['hidden_nominal_code'][$row];
              $quantity  = clean_data($_POST['txt_quantity_'][$row]);
              $item_cost = clean_data($_POST['txt_item_cost'][$row]);
              $line_total = clean_data($_POST['txt_line_total'][$row]);
              $description = clean_data($_POST['txt_description'][$row]);
              $vat_line = clean_data($_POST['txt_line_vat_'][$row]);
              $nominal_id = $id;
              $subtotal = clean_data($_POST['txt_subtotal']);


              $query = ("INSERT INTO acc_posting_details 
              (post_header_id, nominal_acc_no ,quantity,price,line_total,line_vat,vat_total,description,subtotal,debit,credit)
              VALUES 
             ('$post_header_id','$nominal_code','$quantity','$item_cost','$line_total','$vat_line','0','$description','0','$line_total','0')");


              // execute the insert query and push on to the purchase invoice page.
              if ($conn->query($query) === TRUE) {


              Header("Location:../add_purchase_invoice.php");



           } else {
   echo 'Error: '. $conn->error;
        }
          }
           }

              $conn->close();
              ?>

                  <?php 
                    //echo "<pre>";
                   // print_r($_POST);
                   // echo "<pre>";
                   ?>

【问题讨论】:

  • 为什么是代码斜塔?
  • 你有解决我的问题的办法吗?
  • 如果我这样做了,我可能会将其作为答案发布在答案部分下。

标签: php mysql


【解决方案1】:

听起来您的核心挑战是确定您何时处于 foreach 循环的最后一次迭代中,以便您随后可以修改查询。对吗?

您可以使用计数器来提供帮助。大致如下:

$i = 0;
$len = count($_POST['lst_nominal_code']);

foreach ($_POST['lst_nominal_code'] as $row=>$id) {
   // Assign (don't forget to sanitize!) all the POST variables
   <snip>

   if ($i == $len - 1) {
      // This is the last line! We want a different query, with $invoice_gross included
      $query = "...";
   } else {
      // This is NOT the last line, use the default query without $invoice_gross
      $query = "...";
   }

   $conn->query($query);

   $i++;
}

【讨论】:

  • 谢谢你,我认为你在正确的轨道上,我会试一试并报告进展情况。非常感谢。
  • 嗨 jszobody,我今天早上试了一下,但它没有给我预期的结果。包含总金额的行被输入了两次,无论你有多少订单行帖子,只插入了两个。我会尝试分析正在发生的事情,如果我有任何快乐,我会报告。
  • @David 那么你如何实现这个有一些问题。见这里:3v4l.org/8G8ir。使用计数器确定循环的最后一次迭代的方法是合理的,并且效果很好。
  • 感谢 jszobody 的努力,我会再看看这个,因为我怀疑你的方法是正确的。谢谢
猜你喜欢
  • 2016-08-13
  • 2017-06-05
  • 1970-01-01
  • 2012-09-12
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 2017-06-09
相关资源
最近更新 更多