Solved

Mysql insert multiple rows from one (qty) form field

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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