Solved

Insert multiple select data into single database row, not multiple

Posted on 2016-11-27
5
15 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 108

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 108

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now