Solved

Mysql insert multiple rows from one (qty) form field

Posted on 2013-05-25
4
632 Views
Last Modified: 2013-05-26
Hi Experts, I'm building a sample beginner project with assistance from a book Ray Paseur recommended and I've got a question I'm not finding the answer to. I'm wondering if it's possible to perform an INSERT query of two rows based on form data collected from a user if the user types in "2" in a "quantity" field. In context, the form collects data on items that the user wants to donate for disaster-relief. In the pre-populated list of items needed, if the user chooses to donate 2 sleeping bags, is it possible to insert those 2 items as separate objects so the db will auto-increment the ID field?  I hope that makes sense...thanks for your input!
0
Comment
Question by:nikaotech
  • 2
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
ID: 39196612
Yes, the format is:

INSERT INTO yourtable (field1, field2, field3) VALUES ('x','y','z'), ('x','y','z');

If your last auto increment was at say 100 then rows with index 101 and 102 would be created.


Cd&
0
 

Author Comment

by:nikaotech
ID: 39196642
Awesome, thanks!
0
 

Author Comment

by:nikaotech
ID: 39197524
Cobol, if I may, I have a follow up question. In the example above, I mentioned that the number of rows to insert was dependent on the number the user typed in to the "quantity" field. Thus, it will be different for each form submitted. Wouldn't I need a foreach loop or something that would dynamically decide how to handle the number of inserts? Thanks again, I'm doing my best to read and understand but expert help is always easier to put in context and I greatly appreciate it.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39197719
Yeah a loop to generate the query works

Just begin the query string ahead of the loop and then concatenate the value strings in the loop and When you come out of the loop you can execute the query.  However you only have a single value so a for loop makes more sense than foreach.

Cd&
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now