• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 99
  • Last Modified:

Insert multiple select data into single database row, not multiple

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
Black Sulfur
Asked:
Black Sulfur
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
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
 
Black SulfurAuthor Commented:
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
 
Ray PaseurCommented:
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
 
Black SulfurAuthor Commented:
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
 
Ray PaseurCommented:
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now