Scooby08 Posted September 11, 2008 Share Posted September 11, 2008 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? Link to comment https://forums.phpfreaks.com/topic/123712-solved-collecting-array-data-for-database-insert-query/ Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 a simple foreach loop should get you started. foreach($_POST['item_qty'] as $value) { if(!empty($value)) { //Construct your query } } Link to comment https://forums.phpfreaks.com/topic/123712-solved-collecting-array-data-for-database-insert-query/#findComment-638803 Share on other sites More sharing options...
Scooby08 Posted September 11, 2008 Author Share Posted September 11, 2008 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?? Link to comment https://forums.phpfreaks.com/topic/123712-solved-collecting-array-data-for-database-insert-query/#findComment-638818 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 well if you already have the index for the item_tax that you want ......then.............hint hint $item_tax[$key] Link to comment https://forums.phpfreaks.com/topic/123712-solved-collecting-array-data-for-database-insert-query/#findComment-638836 Share on other sites More sharing options...
Scooby08 Posted September 11, 2008 Author Share Posted September 11, 2008 OH my goodness!! So simple.. That's all I was missing.. Thanks a bunch zanus!! Link to comment https://forums.phpfreaks.com/topic/123712-solved-collecting-array-data-for-database-insert-query/#findComment-638837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.