?
Solved

Insert multiple select data into single database row, not multiple

Posted on 2016-11-27
5
Medium Priority
?
68 Views
Last Modified: 2016-11-27
I have managed to perform an insert record which inserts multiple id's from the number of choices selected on a multiple select dropdown. The issue is that it creates a new database record for every selection. So, if I make 4 choices, it creates 4 identical records except for the id. That is the only thing that changes. That doesn't seem like a good way of doing this. I think it is possible to store it just as one record with the id numbers separated by commas but I don't know how that is achieved.

I am running my foreach loop just before my prepared statement beings i.e.:

foreach($_POST['sp_cat_id'] as $sp_cat_id) {
 $stmt = $link->prepare("INSERT INTO.............");
//bind parameters
//execute
//close

Open in new window

0
Comment
Question by:Black Sulfur
[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
  • 3
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41903033
it is possible to store it just as one record with the id numbers separated by commas...
This seems to be a busy day for Separation of Concerns concepts!

Don't do that.  You will find out why, eventually.  Databases are supposed to be relational, structured to support queries, sortable, etc.  When you have a column/row intersection with more than one data element, you've defeated much of what databases are all about.

Consider also the DRY principle, especially if you find your database carrying the same data in many rows.

And for a deeper dive into the design thinking, make a Google search for the exact phrase Should I Normalize My Database and read the very thoughtful arguments on both sides of the question.  Give yourself some time on this topic -- it's a lot to take in with only one sitting!
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41903043
Reading between the lines, are you suggestion that instead of one table, I have 2.  

Instead of one table having the users personal details and their category preferences in one table, their category preferences are stored in another table with just their id and the category id?

e.g.:

customer_id    category_id
         1                        3
         1                        6
         1                       12

Or am I way off?
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41903091
Or am I way off?
Spot ON, if you ask me! :-)

These are often called Junction Tables and there are other names for them, too.  Take a second to think about what you can do with little tables like this...

What if you added a "created" DATETIME column to the table?  You could do trend analysis about the first derivative of interest in any given category!  What if you added a "deleted" DATETIME column to the table?  You could watch for declines in popularity.  Information like this is incredibly valuable for marketing, to wit, understanding your clients interests and the trends in these interests.

If you have proper indexes, queries that use these tables are lightning fast.  

Of course you can achieve identical query results with JOIN clauses, but this comes at the loss of the ability to group the trends over time, and I would never want to lose that kind of information potential.
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41903094
Wow, what a good idea with storing the DATETIME! Does this table need a unique ID? It doesn't seem like it would need one but I am not experienced enough to know if there should or shouldn't be one.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41903100
Yes, you always want to have an AUTO_INCREMENT key in every table.  Traditionally this column is named "id" and defined this way:

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
1

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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.
Suggested Courses

800 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