【问题标题】:How to update multiple table rows into mysql table when click save button using Jquery and PHP?使用Jquery和PHP单击保存按钮时如何将多个表行更新为mysql表?
【发布时间】:2021-10-23 19:19:45
【问题描述】:

我有一个 myql 表名 - invoice_details

invoice_number received_amount receiptID
1000               0.00         
1001                0.00
1005                0.00

我有一个 html 表格

点击保存按钮时,用收到的金额和receiptID 更新invoice_details 表,其中html 表行中的发票编号(1001,1005) html 表中会有多行。

附加此html表格代码:

$('#invoicelist_receipt').find('tbody').remove();
$.each($("input[name='myTextEditBox[]']:checked"), function() {
    var data = $(this).parents('tr:eq(0)');
           
    //  values += $(data).find('td:eq(0)').text() + "," + $(data).find('td:eq(1)').text() + "," + $(data).find('td:eq(2)').text() + ",";   
    var t1 = $(data).find('td:eq(0)').text();//invoice date
    var t2 = $(data).find('td:eq(1)').text();//invoice no
    var t3 = $(data).find('td:eq(2)').text();//invoice amt
    trtoggle += "<tr><td class=''>" + t1 + "</td><td name='invoice_no_receipt[]' class='invoice_no_receipt'>" + t2 + "</td><td class=''>" + t3 + "</td><td class=''><input class='form-control invoice_amt_receipt' type='number' data-type='invoice_amt_receipt' id='invoice_amt_receipt_1' name='invoice_amt_receipt[]' for='1'/></td></tr>";    
    //values.push({ 'invoicedate':$(data).find('td:eq(0)').text(), 'invoiceno':$(data).find('td:eq(1)').text() , 'invoiceamount':$(data).find('td:eq(2)').text()});             
});
$("#invoicelist_receipt").last().append(trtoggle);  

点击按钮时:(创建新收据)

var invoice_no_receipt  = []; //where invoice no = 1000,1001,1005 etc..
var invoice_amt_receipt  = [];//this received amount i have to update into database - invoice details table
        
$('.invoice_no_receipt').each(function() {
    invoice_no_receipt.push($(this).val());
});

$('.invoice_amt_receipt').each(function() {
    invoice_amt_receipt.push($(this).val());
});
    
$.ajax({
    url: base_url + "index.php/welcome/savereciptfinal/",
    type: "POST",

    data: {
        "getinvnumber": invoice_no_receipt,
        "getinv_recived_amount": invoice_amt_receipt
    },
    success: function(data) {

    }
});

PHP Codeigniter 代码

public function savereciptfinal()
    $value2 = "0001"; //autogenerate number
    $value  = $value2;
    $data = array(
        'rece_No' => $value
    );

    $insert_id = 0;
    if ($this->db->insert("receipt_details", $data)) {
        $insert_id = $this->db->insert_id();
    }

    $data2 = array(
                'rece_Amt' => $this->input->post('getrece_amt'),
                'receipt_ID' => $value2; //the above auto generated number i need to update invoice_details for column receiptID
            );
    $this->db->where('invoice_No ', $inv_id);
    $this->db->update('invoice_details', $data2);
}

【问题讨论】:

  • 请不要只要求我们解决问题或为您编写解决方案。欢迎初学者,但我们希望您付出一些努力在提出问题之前解决您自己的问题。 SO 不是免费的编码服务,尽管我们非常愿意帮助您解决您编写的代码的问题。
  • @RiggsFolly 我已经更新了我的代码。你能帮我做这件事吗?单击保存按钮时需要更新mysql数据中的多行每行更新行
  • 您好,欢迎来到 StackOverflow。请花点时间阅读这篇关于如何提问的文章stackoverflow.com/help/how-to-ask 也阅读这篇关于如何以最低要求提出一个好问题的文章stackoverflow.com/help/minimal-reproducible-example

标签: javascript php jquery codeigniter


【解决方案1】:

您可以从此代码中获得灵感,并将其与您自己的代码同步

首先清除这个脚本:

$('#invoicelist_receipt').find('tbody').remove();

$.each($("input[name='myTextEditBox[]']:checked"), function() {
    var data = $(this).parents('tr:eq(0)');  
    var t1 = $(data).find('td:eq(0)').text();//invoice date
    var t2 = $(data).find('td:eq(1)').text();//invoice no
    var t3 = $(data).find('td:eq(2)').text();//invoice amt
    trtoggle += "<tr><td class=''>" + t1 + "</td><td name='invoice_no_receipt[]' class='invoice_no_receipt'>" + t2 + "</td><td class=''>" + t3 + "</td><td class=''><input class='form-control invoice_amt_receipt' type='number' data-type='invoice_amt_receipt' id='invoice_amt_receipt_1' name='invoice_amt_receipt[]' for='1'/></td></tr>";    
                
});
$("#invoicelist_receipt").last().append(trtoggle); 

 

设置您的保存按钮 ID 而不是“your_save_button_id”

您可以在js中查看如何发送您的发票详细信息

$(document).on('your_save_button_id', 'click', function(){ 

    var invoice_no_receipt  = []; 
    
    $('.invoice_no_receipt').each(function() {
    
        // Json format to add product invoice detail
        var invoice_detail = new Object();

        // Get invoice number
        invoice_detail.no = $(this).text();
        
        // Get parent tr
        var parent_tr = $(this).closest('tr');

        // Get amount
        invoice_detail.amt = $(parent_tr).find('invoice_amt_receipt').val();

        // Add invoice detail to invoice_no_receipt
        invoice_no_receipt.push(invoice_detail);

    });
        
    $.ajax({
        url: base_url + "index.php/welcome/savereciptfinal/",
        type: "POST",
    
        data: {
            "invoice_no_receipt": invoice_no_receipt
        },
        success: function(data) {
    
        }
    });
})

您可以查看如何在 PHP 中获取您的发票详细信息

在 ajax 函数中输入你的 PHP 代码

// Get invoice_no_receipt
        $invoice_no_receipt = $_POST('invoice_no_receipt');
    
        foreach( $invoice_no_receipt as $item )
        {
            // Get invoice number
            $invoice_no = intval($item['no']);
            
            // Get invoice amount
            $invoice_amt = floatval($item['amt']);
            
            // Update your invoice in db one by one and by use from these variable
            // Your update function
        
        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 2012-05-21
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
相关资源
最近更新 更多