Solved

Mysql insert multiple rows from one (qty) form field

Posted on 2013-05-25
4
639 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

820 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