Handling one-to-many references in MySQL - php

I didn't know how to give title to this but I have the following database:
accidentDetain(id, location, weather_conditions desc (and few more columns));
weatherConditions(id, title)
acc_weat_cond(id, wc_id, ad_id)
wc_id = weatherConditions ID, ad_id = accidentDetain ID
Now the situation is the user can store multiple weather conditions such as (rain, wet, snow Ice Fog etc)
Let's say user chooses 3 out of those 6 options and those will be stored in acc_weat_cond table, with accident Detail id and weather conditions id.
After saving, the user decided to change and to unchecked one of the option and then presses the save button. The issue is, there are 3 records already stored into acc_weat_cond table how would I would change and make them to two records.
Will I have to delete the first records from the database and then store again newly checked options? Or is there any easier way doing the above mentioned situation.
One last option is that I violate the role of database normalization and stored directly in the accidentDetails table and separate the values with a comma.
Feel free to ask if any more information is required...

I would have an <input type='hidden'name='checkedflds' value='1,2,3' />-field which contained the values that were checked before the user updated. Then after postback, you can compare the new list against this and will easily see what additions he made and which elements he removed...

I would call all of the options and compare to what is checked, and delete what you need. If you store it on the form, then there is a potential for out of date data.

Related

How to save the modifications of a column in sql

I'm coding a website where users can add concerts/events and other users can modify the informations provided for every concert/event added on the website.
I want to save every modification that has been made and the user that made the modification. For instance there is this functionnality on Wikipedia where every article has saves of every modification made and the user who made it.
For now, I just save the name of the user who made the last modification(s), and the modifications aren't saved: when a modification is made it will just overwrite the previous value of the corresponding column.
You can see under the current state of the tables I'm talking about. One for concerts with different informations: artist, date of the event, hour, links related to the event etc...
and another one for users.
Current sql tables
I think a first step would to create separate tables for every element of a concert that can be modified, with 2 foreign keys (one linked to user, one linked to concert) plus the value of the element. But still, if a modification is made I still don't know how to save the previous value and previous user.
I've thought of puting values inside an array but I don't know if this is possible in SQL and if this is the best solution.
Thanks for reading/helping
You should add a column that mark the create/update time to the concert table.
Then create a new history table (same schema of concert) for storing concert changes.
When a user edit a concert:
Save the current state of concert content to the history table.
Update the new change to the concert table with the user_id of the user who perform this action
That's it! When you want to list the changes history, just query the history table!
If you want to log the delete operations as well, add another column and mark the end period of each version (a little bit more complex)

Database set up for multi-way relationships and form data collecting

I've posted a few questions on here and have gotten very great help and support. I'm still fairly new to programming and I'm putting together what I thought would be a simple website for the company I work at. I apologize in advance for my lengthy post/question, I just want to be thorough and clear in what I'm asking. My question is more of needing some help getting pointed in the right direction of how to get started and some best practices to be aware of. What I'm working on right now is to create a system where a user can submit a questionnaire/online form to inquire about a specific product (in this case it's a hard money loan product). The way I am planning on setting it up is to have a database with multiple tables (users, user_info, loan_app, property) and connect these together by referencing each other. I've read about table joins and I understand them conceptually but I have no idea how to implement in practice. I've had a hard time finding actual examples.
Specifically, this is what I am doing and how I am thinking it should work (correct me if I'm wrong or if there's a better way to do it):
1- the user (aka the borrower) signs in to the website. The user log in system references the user table where things like first name, last name, user name, password and user ID are stored. I have included an "active" column in this table so that when a user logs in the condition for them to get into the website is that the username and password match AND the user is activated. This way we can control on the back end certain user accounts access. I have this part working.
2- when the user registers, they only fill out the information that creates a new record in the "user" table. I have created a second table called "user_info" that will contain other data like home address, phone number email etc. But I need to be able to associate the correct record with right user. This is my first issue to wrap my head around. My thinking behind doing this instead of simply putting all this information in the user table is that for one, I might keep adding to that table and make it very big, and two for security reasons, I would like to keep the information separate. I don't know if this thought process has any merit to it though. Again, that's why I'm posting this here.
3- The user, once logged in, clicks on a button on their home screen/dashboard that will take them to the loan "pre-approval application" form, which is the questionnaire. On this form their basic information will be echoed/posted from the "user_info" table to pre-populate certain fields like first name, last name, email, phone number, address etc. So going back to #2 making sure I can associate the user with the correct record in the "user_info" table is critical. THEN, there are additional fields that the user has to fill out in order to submit the application/questionnaire. These form fields will create a new record in the "loan_app" table. This table will have a loanid column that is the primary key for that table, and an auto generated/randomized 6 or 7 digit loan number (loannum). The loanid will be a hidden value but the loan number will be like a reference number that is associated with the loan for the life of it and used for later accounting and recording purposes internally, whether or not it actually becomes a loan. The loanid, I'm assuming here, is the Foreign key in the "user" table and the userid is the Foreign key in the "loan_app" and "user_info" tables correct? If so, how do I incorporate being able to simultaneously associate all these records when the loan application/questionnaire is submitted? My thought would be write individual php scripts that does each of these things separately then have a "master" php that includes all of those individual ones that is placed as the form action associated with the submit button on the form.
Thanks for taking the time to read through this. I'd really appreciate any advice or reference material that I can read up on to learn more about this stuff. My job has a pretty crazy schedule and I travel a lot so I don't have the time to take actual classes to learn this stuff formally. I'm pretty much doing this as I go.
Also, I'm using MAMP with mysql, not sure if that helps any or not...
The user table's primary key userid can be the primary key of the user_info table as well, since each user will have only one user_info record, right? A foreign key constraint is good to ensure only valid userids get recorded in user_info.
The loan_app table can contain a denormalized relationship from loanid to userid so that each loan application is associated with a user. Again, use an FK constraint for integrity.
Don't include loanid in the user table - that would mean each user has a relationship to a single loan application. You already have the one-to-many relationship you need in the loan_app table.

User's custom profile fields

I am currently working on a system that would allow users to add additional custom fields for the contacts that they add.
I wondered what is the best and most efficient approach to add such ability?
Right now what I was thinking was to have 1 table per users (with foreign keys to a "main" contacts table) and then adding a column for each custom fields that the user adds (since I don't expect to have more then 100-200 users per database shards [sharding is easy since every users never see each-other's content in this system]), although I am not 100% sure that this would be the right solution for such problems.
Maybe you could try to have one separated table to store a reference to the user, plus the field name and value, this way you will be able to have lots of custom fields.
If you go with Boyce-Codd, you separate the information and store them into a table.
Means one table for all users with a foreign key.
One table per user would lead to hundreds or more tables with possible repeated information.
You need to have one table named USERS that stores the id of a user and fixed info you might want. Then, you could have a CONTACT table, that stores the type of contact user might create, and one matching table USER_CONTACT that matches the user unique id with the id of the contact that was created.
With this, you could have advanced data mining on all the information stored, like nowing how many contacts each user created, who created more, etc...

Generic structuration of a web form and a MySQL's table for saving a array of undefined length

In a web form i will ask the user for their job experiences, this data will have no fixed lenght. I need to let the user insert all the items he needs, every item will content 3 fields; job title, description and year.
My firts problem is, how can i ask in the html form for the items? i mean, whats the best way to ask items with no fixed lenght using html/php (and maybe ajax)? I saw some sites that have a button (add one) when you hit it a new item slot is showed, but i have no idea of how to implement this, an example will be sufficient.
The second part is, how can i managed the data flow in post or get?, until now, i only use fixed fields, so i always know in my php script how many post or get vars i will get. How can i use multiple POST vars without knowing the amount of them?
And the last one (and the more important), how will be the best structure for my table in MySQL? If i get multiple items for a fixed table where i will have all my users, how can i resolve the multiple items issue? For example, if my table is:
User | password | job_experiences
admin | root | (this is just a cell, how can i save multiple items here?)
jonh | 1234 | (this is just a cell, how can i save multiple items here?)
Thanks for any help!!!!
Those are 3 questions, and it's best to post 3 questions, instead of discussing all of them. I will post the basics, and if you have specific questions, ask.
First, use button to add, and a JavaScript to clone an existing row (which can have more then one input field). For fieldnames use something like company_name[] - the [] is the important part, at this will send the field as an array. If you are editting profile, you can use company_name[$id] to preserve the mapping.
Second, in PHP you will receive this as $_POST['company_name'] which will be numeric array with all the company names. Or if you specify $id - with the corresponding keys. So, you have to loop trough all company_names, if there are other fields - you retrieve them the same way, using the current key. Example:
for (i =0; i<$_POST['comany_name'].length;i++) {
$company = $_POST['comany_name'][$i];
$start_year = $_POST['from'][$i];
...
}
Next, you need 1 table for the users (username, password), and another for job experiences (userid, company, description, from, to). This is called 1:M relation

Retrieve a table's fields from a lookup table dynamically

I am making a mysql/php project and in one form, the specs require dynamic fields in a way that you have an initial selectbox with a value from a lookup table that describe job unions. Depending on the selected value, it will spawn (probably via reflection) different fields.
For example, I have 2 associations with ids 1 and 2. If the user selects union #1, then the fields would be first name, last name, phone, address. If the user selects union #2 then the fields would be mobile, email, im name, "enroll now?"(checkbox).
Now, this might occur often, because the total of unions are more than 10 and specs require it to be flexible.
What I thought is this:
form loads up the fields of the first lookup (jobunions)
user selects the job union, and the value of the selectbox is the name of another table, for example LK_TABLE_2
The reflection examines the LK_TABLE_2 fields and retrieves/renders the fields below the selectbox of step 1.
I need your opinion on whether this business logic is acceptable (is there a pattern I could use?), if not anything to suggest and if it's doable how to store the filled data into the user preferences table.
Any insight would do.
Update: here is a schema (pdf) of what I am trying to do
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B_vptVa0K8J2YjBjMGJmZDgtYzUxZi00ZTE5LTgxZjgtOTNlMjQ5OGM3ZTY1&hl=en_US
I am closing this question, because of the project that was terminated, and further more I don't work in the specific company anymore (over half a year ago).
We jumped to a more nosql solution before this happened and it was working fine... until termination.

Categories