I think I'm finally at the last bit of this invoice creation tool I'm creating, so everything is passed from JSON aJAX which is all passing to the PHP function fine however I'm note sure ive got the loop working for the invoice items.
You will see the multiple products I need to loop to add to invoice_items table.
invoice_product[]
etc.. in results below - it's actually not adding anything and return the JSON error (bottom of PHP code) so not sure if it's because of that.
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();
}
Your loop should be like this:
... define fixed/unchanging values...
foreach(...) {
... define changing values ...
execute query
}
As is, your foreach loop goes through ALL of your data values, overwriting the ones you saved in the previous iteration. Then when it comes time for you to run your INSERT query, you have only the LAST set of data to insert.
Running the query inside the loop will let you save each "row" of values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.