?
Solved

Mysql insert multiple rows from one (qty) form field

Posted on 2013-05-25
4
Medium Priority
?
652 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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