• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

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.
0
rgranlund
Asked:
rgranlund
  • 5
  • 3
  • 3
  • +1
1 Solution
 
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
 
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now