php - 在 PHP 中循环名称属性 [] 以从 JSON 插入数据库

标签 php mysql

我想我终于到达了我正在创建的发票创建工具的最后一点,所以所有内容都是从 JSON aJAX 传递的,这一切都很好地传递给 PHP 函数,但是我注意到我确信循环正常工作对于发票项目。

您将看到我需要循环添加到invoice_items 表的多个产品。

invoice_product[] 等......在下面的结果中 - 它实际上没有添加任何内容并返回 JSON 错误(PHP 代码的底部),所以不确定是否是因为这个。

传递数据的示例:

action  create_invoice
customer_address_1  5 Some Road
customer_address_1_ship 5 Some Road
customer_address_2  Epping
customer_address_2_ship Epping
customer_county Essex
customer_county_ship    Essex
customer_email  james@ambientlounge.com
customer_name   James Brandon
customer_name_ship  James Brandon
customer_phone  0748646013845
customer_postcode   CM16 8DD
customer_postcode_ship  CM16 8DD
customer_town   Epping
customer_town_ship  Epping
invoice_date    14/05/2015
invoice_discount    130.50
invoice_due_date    20/05/2015
invoice_id  AMBMN0001
invoice_product[]   Versa Table - Wildberry
invoice_product[]   Versa Table - Aubergine
invoice_product[]   Versa Table - Tundra Spring
invoice_product_discount[]  10%
invoice_product_discount[]  
invoice_product_discount[]  70.50
invoice_product_price[] 200
invoice_product_price[] 200
invoice_product_price[] 200
invoice_product_qty[]   3
invoice_product_qty[]   2
invoice_product_qty[]   1
invoice_product_sub[]   540.00
invoice_product_sub[]   400.00
invoice_product_sub[]   129.50
invoice_shipping    23
invoice_subtotal    1069.50
invoice_total   1092.50
invoice_vat 213.90

PHP

// Create invoice
if ($action == 'create_invoice'){

    // invoice customer information
    // billing
    $customer_name = $_POST['customer_name']; // customer name
    $customer_email = $_POST['customer_email']; // customer email
    $customer_address_1 = $_POST['customer_address_1']; // customer address
    $customer_address_2 = $_POST['customer_address_2']; // customer address
    $customer_town = $_POST['customer_town']; // customer town
    $customer_county = $_POST['customer_county']; // customer county
    $customer_postcode = $_POST['customer_postcode']; // customer postcode
    $customer_phone = $_POST['customer_phone']; // customer phone number

    //shipping
    $customer_name_ship = $_POST['customer_name_ship']; // customer name (shipping)
    $customer_address_1_ship = $_POST['customer_address_1_ship']; // customer address (shipping)
    $customer_address_2_ship = $_POST['customer_address_2_ship']; // customer address (shipping)
    $customer_town_ship = $_POST['customer_town_ship']; // customer town (shipping)
    $customer_county_ship = $_POST['customer_county_ship']; // customer county (shipping)
    $customer_postcode_ship = $_POST['customer_postcode_ship']; // customer postcode (shipping)

    // invoice product items
    foreach($_POST['invoice_product'] as $key => $value) {
        $item_product = $value;
        // $item_description = $_POST['invoice_product_desc'][$key];
        $item_qty = $_POST['invoice_product_qty'][$key];
        $item_price = $_POST['invoice_product_price'][$key];
        $item_discount = $_POST['invoice_product_discount'][$key];
        $item_subtotal = $_POST['invoice_product_sub'][$key];
    }

    // invoice details
    $invoice = $_POST['invoice_id']; // invoice number
    $invoice_date = $_POST['invoice_date']; // invoice date
    $invoice_due_date = $_POST['invoice_due_date']; // invoice due date
    $invoice_subtotal = $_POST['invoice_subtotal']; // invoice sub-total
    $invoice_shipping = $_POST['invoice_shipping']; // invoice shipping amount
    $invoice_discount = $_POST['invoice_discount']; // invoice discount
    $invoice_vat = $_POST['invoice_vat']; // invoice vat
    $invoice_total = $_POST['invoice_total']; // invoice total

    // insert invoice into database
    $query = "INSERT INTO invoices SET
                invoice = '".$invoice."',
                date = '".$invoice_date."',
                due_date = '".$invoice_due_date."',
                subtotal = '".$invoice_subtotal."',
                shipping = '".$invoice_shipping."',
                discount = '".$invoice_discount."',
                vat = '".$invoice_vat."',
                total = '".$invoice_total."';
            ";

    // insert invoice items into database
    $query .= "INSERT INTO invoice_items SET
        invoice = '".$invoice."',
        product = '".$item_product."',
        qty = '".$item_qty."',
        price = '".$item_price."',
        discount = '".$item_discount."',
        subtotal = '".$item_subtotal."';
    ";

    // insert customer details into database
    $query .= "INSERT INTO customers SET
                name = '".$customer_name."',
                email = '".$customer_email."',
                address_1 = '".$customer_address_1."',
                address_2 = '".$customer_address_2."',
                town = '".$customer_town."',
                county = '".$customer_county."',
                postcode = '".$customer_postcode."',
                phone = '".$customer_phone."',

                name_ship = '".$customer_name_ship."',
                address_1_ship = '".$customer_address_1_ship."',
                address_2_ship = '".$customer_address_2_ship."',
                town_ship = '".$customer_town_ship."',
                county_ship = '".$customer_county_ship."',
                postcode_ship = '".$customer_postcode_ship."';
            ";

    header('Content-Type: application/json');

    // execute the query
    if($mysqli -> query($query)){
        //if saving success
        echo json_encode(array(
            'status' => 'Success',
            'message' => 'Invoice has been create successfully!'
        ));
    } else {
        // if unable to create invoice
        echo json_encode(array(
            'status' => 'Error',
            'message' => 'There has been an error, please try again.<pre>'.$query.'</pre>'
        ));
    }

    //close database connection
    $mysqli->close();

}

最佳答案

你的循环应该是这样的:

... define fixed/unchanging values...
foreach(...) {
   ... define changing values ...
   execute query
}

照原样,您的 foreach 循环将遍历所有数据值,覆盖您在上一次迭代中保存的数据值。然后,当您运行 INSERT 查询时,您只需插入最后一组数据。

在循环内运行查询将允许您保存每“行”值。

关于php - 在 PHP 中循环名称属性 [] 以从 JSON 插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30083586/

相关文章:

java - Spring Batch自定义数据库模式初始化

php - 如何通过单个查询检查多个 ID 是否存在

Mysql不在大表上使用索引

php - 我将如何通过 REST API 处理游戏操作?

php - 限制表格行中的列数

php - 为什么 PHPUnit 不接受 XML 定义的测试套件?

php - 在 where 子句中找不到列

mysql - 结果包含多行

php - Phalcon db 连接到同一主机上的多个数据库

php - 无法使用 Debian 连接到 SSL Postgres 外部数据库(证书权限错误)