Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mysql insert multiple rows from one (qty) form field

Posted on 2013-05-25
4
Medium Priority
?
655 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 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