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.
LVL 7
rgranlundAsked:
Who is Participating?
 
Barry62Connect With a Mentor Commented:
Sorry, I just noticed my mistake.  The for should be:

for($x=0;$x<$qty;$x++){

Open in new window



As for your question, I am assuming that the form has a field for quantity.  If the customer enters 13 for the quantity, that is what the POST picks up and assigns to $qty.  The for loop will execute the insert statement 13 times.
0
 
Barry62Commented:
How is your form laid out?  Also, how is your data structured?  How does your program assign different productID's to each identical item?
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
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.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Barry62Commented:
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

0
 
Ray PaseurCommented:
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.
0
 
rgranlundAuthor Commented:
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

0
 
Ray PaseurCommented:
Show us the order form, please.
0
 
rgranlundAuthor Commented:
I don't actually have it.  It is handled by Google Checkout widget.
0
 
Ray PaseurCommented:
Understood.  I think what Barry62 suggests is right.  Best of luck with the project, ~Ray
0
 
Barry62Commented:
Does my solution work for you?
0
 
rgranlundAuthor Commented:
I just got done implementing.  Thanks, works perfect.
0
 
Barry62Commented:
Thanks for the points!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.