Solved

Insert multiple select data into single database row, not multiple

Posted on 2016-11-27
5
43 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
  • 3
  • 2
5 Comments
 
LVL 109

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
 

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 109

Accepted Solution

by:
Ray Paseur earned 500 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
 

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 109

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

808 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