mercredi 6 mai 2015

Looping name attribute[] in PHP to INSERT into database from JSON

Think im finally at the last bit of this invoice creation tool im creating, so everything is passed from JSON aJAX which is all passing to the PHP function fine however im note sure ive got the loop working for the invoice items, can someone help me?

You will see the multiple products i need to loop to add to invoice_items table.

invoice_product[] etc.. in results below - its actually not adding anything and return the json error (bottom of PHP code) so not sure if its because of that but if anyone can spot in the PHP that might be causing that would be great as well :)

Thanks in advance for help on this!

Example of data being passed:

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();

}

Aucun commentaire:

Enregistrer un commentaire