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

x
?
Solved

Insert multiple select data into single database row, not multiple

Posted on 2016-11-27
5
Medium Priority
?
82 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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

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