Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

MySQL INSERT

Is there a way to dynamically create an INSERT Statement?  I have the following issue:

When a customer orders a product and they order 10 of the same product.  Each product has a unique product ID and and unique information.  Each of the products ordered needs to be inserted on its own.  So if someone ordered 10 items I would need to have 10 insert statements.  

Is there anyway to write the insert statement so it loops and inserts however many times the quantity is set to?

So if a customer ordered 37 items, the insert statement would automatically loop 37 times.
Avatar of Barry62
Barry62
Flag of United States of America image

How is your form laid out?  Also, how is your data structured?  How does your program assign different productID's to each identical item?
You should already be creating the INSERT statement dynamically if you're using PHP. How are you presenting the product IDs now? If you're presenting them in an array, you'd just loop through the array in a foreach...as loop.
OK, assume that the variable $qty is posted as the number of items ordered.

if (isset($_POST['submitbutton'])){
  $qty = $_POST['qty_ordered'];
  for($x=;$x<$qty;$x++){
     mysql_query("INSERT INTO...);
  }
}

Open in new window

Show us the order form, please.  Just post a URL link to it.  From the input elements of the form we will be able to show you how to create the query you need.
Avatar of Robert Granlund

ASKER

if have the following code, how do I make it loop 13 times if a person orders 13 units?

if (isset($_POST['submitbutton'])){
  $qty = $_POST['qty_ordered'];
  for($x=;$x<$qty;$x++){
$q=("INSERT INTO orders (order_id, order_date, item_name, unit_price, sku) VALUES ('', '$time_stamp','$item_name', '$unit_price', '$sku')") or die(mysql_error());	
				$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: ".mysqli_error($dbc));
				if (mysqli_affected_rows($dbc) == 1) {  //  INFO INSERTED INTO ORDER_ITEMS  //
				echo '<span class="good">Items ave been entered into the ORDER_ITEMS: Cards not e-cards <br />';
				}  //  END INFO INSERTED INTO OREDER_ITEMS
  }
}

Open in new window

Show us the order form, please.
I don't actually have it.  It is handled by Google Checkout widget.
ASKER CERTIFIED SOLUTION
Avatar of Barry62
Barry62
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Understood.  I think what Barry62 suggests is right.  Best of luck with the project, ~Ray
Does my solution work for you?
I just got done implementing.  Thanks, works perfect.
Thanks for the points!