Jump to content

[SOLVED] Collecting array data for database INSERT query.


Scooby08

Recommended Posts

I'm not really sure how to even explain this, but here goes..  I have a form dynamically built from the database that lists items for one to choose from.. Here is the part of the form I am having troubles writing an INSERT query for..

 

<form action="" method="post">  
    <label>Item 1 - </label>
    Qty <input type="text" name="item_qty[1]" width="35" />
    Tax <select name="item_tax[1]">
        <option value="">---</option>
        <option value="10">10%</option>
    </select><br />
    
    <label>Item 2 - </label>
    Qty <input type="text" name="item_qty[2]" width="35" />
    Tax <select name="item_tax[2]">
        <option value="">---</option>
        <option value="10">10%</option>
    </select><br />
    
    <label>Item 3 - </label>
    Qty <input type="text" name="item_qty[3]" width="35" />
    Tax <select name="item_tax[3]">
        <option value="">---</option>
        <option value="10">10%</option>
    </select><br /><br />
    
    <input type="submit" name="submit" />
</form>

 

The "1" in "item_qty[1]" is the item_id, and same for the others; "item_qty[2]", "item_qty[3]".. The thing is I need to loop through all items and find only ones with a quantity entered and insert those into the database, otherwise do nothing with them..

 

This is what the query eventually needs to be for only items with a quantity entered:

 

$q  = "INSERT INTO order_items (item_id, item_qty, item_tax) VALUES ('$item_id', '$item_qty', '$item_tax') ";

 

Any ideas out there I can try?

Yes that is where I am stuck at.. I can get all but the tax into the query.. I do get a tax array, but I'm not sure how to break that apart to get the value for that field because its already in a loop.. I can do a separate loop, but dont know how to combine them.. Here is my code and query echoed:

 

<?php
if (isset($_POST['item_qty'])) {
$item_qty = $_POST['item_qty'];
$item_tax = $_POST['item_tax'];

foreach ($item_qty as $key => $value) {
	if (!empty($value)) {
		$q = "INSERT INTO order_items (item_id, item_qty, item_tax) VALUES ('$key', '$value', '$item_tax') ";
		echo $q."<br />";
	}
}
}
?>

 

the echo:

 

INSERT INTO order_items (item_id, item_qty, item_tax) VALUES ('3', '2', 'Array')

 

So I get the item_id, item_qty, but cannot figure out how to display the tax??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.