I'm making an online game for a mounth i guess. I made user profiles, forget password, manage your account pages. And when they enter the game they can see their names and some information abut each other. Never mind of that.
Now, i need to do items for each users. So, here is my mysql table:
user_id | name | email | password | color | items | head | face | body | hand | feet | created
But i can add just one value into items col. How can i add all the item id's the user have? Please someone help!!!
You need a second table. Remove the "items" field from this table and create a new table called Items. In it you will have:
primaryKey | user_id | item
Then to get every item a given user has, you perform a SQL SELECT WHERE user_id=#id and iterate over the results.
There is a one to many relationship between the user and the number of items the user can have. You need to create another table with the following columns
user_id
item_id
item_desc
and store the item information there
You can store the ids as serialised arrays or create an user_item table.
Related
Information:
I have 2 tables(users for the users of the website and new_themes for themes/posts(whatever) created by the users.
!!!There is no relationship between these 2 tables.(A user can create a new post if he is logged in)
Tables:
Users:
1 id
2 name
3 email
4 password
New_themes:
1 id
2 title
3 text
4 upVotes
5 downVotes
6 TagName
What I want to do:
I created the feature that user can like the post(but he can like it unlimited number of times) but I want them to be able to like just once in a life time a post.
What I can't figure out is the idea/structure/logic of how to make it. I am thinking of creating a table . A "pivot" one for tables users and new_themes(many to many relationship in Laravel)
1 id_user
2 id_theme
Where the both of them have a composite primary key so they won't be any duplicate values, so if the user wants to vote once again the same post he won't because there can't be duplicate values.
Here is the problem:
If this is the solution, I have doubt and no idea on how to populate the pivot table when a user likes a theme. If this isn't the solution any advice or idea will be very helpful for me to finish my last feature of my wep app.
Thank you very much.
What I can't figure out is the idea/structure/logic of how to make it. I am thinking of creating a table . A "pivot" one for tables users and new_themes(many to many relationship in Laravel)
That's the way to go. So it would look like this:
+---------+----------+
| user_id | theme_id |
+---------+----------+
| 1 | 1 |
+---------+----------+
| 12 | 1 |
+---------+----------+
| 14354 | 1 |
+---------+----------+
To 'like' a theme, just insert the user and theme id into the table. The rest is just taking care of the response from the database - giving the user feedback for his try to like something more than once etc.
And of course you should not forget the ability to unlike - so you need to delete from the table again... if you want users to unlike.
First you have to create a new table with following fields (id, user_id, theme_id, status) as you said, maintain the relation between user and theme in that table.
when user like the theme insert a record with status 1 , if user unlike the theme, first check the user_id in the table, if user id is present then just update the status to 0 otherwise insert new entry with status 1.
I am working on a shopping cart website,
I created a table to display to the user tp order like this:
+---------------------------------+
| My order list |
+----+------------+---------------+
| ID | NAME | ACTIONS |
| id | name | update delete |
| id | name | update delete |
+----+------------+---------------+
The problem is I identify the edit item like this:
mydomain.com/product_list/edit/1
Where 1 is the table ID of the product list
It is really dangerous as other user can delete the records of other by trying to tamper the id. How to secure this one? Even by posting, the hacker can just create a fake posting page, and post the fake id to the program.
Thanks
I've read your question and comments. And I think storing owners of tables in another table can help you (if I get you right)
You said
since the order id is just by number , e.g. order id 1 is the first record for customer A , order id 2 is for B, 3 for A etc... so if user B type in / post 3 as parmeter then he can delete the record of user A
Before you let me alter the table, you have to check if I've permissions for this. You can check my e-mail, my password, add them to session etc. But when it comes to check query string, you can simply look if this table is one of my tables.
So if user B type in / and post 3 as parameter, you can go and check if B is one the owners of table 3. If no, don't let B do anything.
I have a dynamic form that creates some inputs that will generate values and should be saved into the database. Each set of values should be saved separetely in a single field in the database called "Education":
Should be stored like this:
+--------+---------+----------+--------+--------+--------+--------+-----+
| id | name | Education |
+--------+---------+----------+--------+--------+--------+--------+-----+
| 100 | John | [Harvard, Marketing,2009,2014] [MIT,CS,2005,2009] |
+--------+---------+----------+--------+--------+--------+--------+-----+
| 101 | Daniel | [TEC, Marketing,2009,2014] [Standford,CS,2001,2005]|
+--------+---------+----------+--------+--------+--------+--------+-----+
The Education field can have up to 10 sets of values, I'm just showing 2.
Please look at the JSFIDDLE to see how it actually works: http://jsfiddle.net/YueX2/6/
How can I store into the database when a single set of values is edited and saving it into the database only updating the given set of values?
Also, is this the best way to do it?
There is a practice in designing databases called normalization which would lead you to the best way to go about it. Based on your Question and jsFiddle you would end up with 2 separate database tables.
Ex: tbl_Users: which would contain fields such as
userID
userName
Then you would have another table Ex: tbl_Education which would contain a few fields such as
record_id
userID
schoolName
It is in this table where you would set the particular users id in the userID field which would have to match the userID field from tbl_Users and then a single school they attended in the schoolName field. If they attended multiple schools, they would have multiple entries in the tbl_Education table but only a single entry in the tbl_Users table. If you need to retrieve the data you would perform a SQL query on the two table and join on the User_id field. This would result in multiple records being returned, but with all of the data needed.
Any information which is specific to the particular part of their education would go in the tbl_Education table and anything specific to the user (hair color, eye color, height, etc) would go in the tbl_Users table.
Ex SQL Query:
SELECT tbl_Users.userID, tbl_Users.userName, tbl_Education.schoolName
FROM tbl_Users, tbl_Education
WHERE tbl_Users.userID = tbl_Education.userID;
The WHERE clause is essentially the join between the two tables. There are many ways to write this query, I used the method which seems visually the easiest to see what is going on.
Here is the wikipedia link for normalization to get you started.
http://en.wikipedia.org/wiki/Database_normalization
This is not the best way to solve this problem. You better make 2 tables.
Table users:
+--------+---------+
| id | name |
+--------+---------+
Table education:
+--------+----------+------------+-------+-----+------+
| id | location | discipline | start | end | user |
+--------+----------+------------+-------+-----+------+
As education.user you save a foreign key users.id from the user table. the implementation of the input form is on the one hand side more complex but you have no limitations of entrys per user nor too much overhead in your database.
This is a bit confusing. I cant find how to word it for google and I just cant wrap my head around the logic to do this.
I have "contacts" and "sites" tables that I am storing data for in a database. We need to have a page showing the contacts information and what "sites" they are associated with AND have a page for information about a "site" and show what contacts are associated with it.
Right now I have a field for "contacts" that has comma separated ids of each site that its associated with and a field for "sites" that also has comma separated ids of each contact associated with that site.
When I create a new site with an associated contact. how should the logic go that will update the "associated sites" field on the contacts row in MySQL while also updating its own "associated contacts" field?
I think, you are speaking about many-to-many relationships.
I assume you have a table design like this:
tbl_contacts tbl_sites
id | full_name id | label
1 | John Doe 1 | my website
3 | Maria Doe 2 | super website
You need a table to "link" the tables. this is a many-to-many-table:
tbl_contacts2sites
id | contact_id | site_id
1 | 1 | 2
5 | 1 | 1
3 | 3 | 2
So, John Doe is assigned to both sites, but Maria only to the "super site".
This is the common way to design your relationships. You should avoid any comma seperated lists for this kind of relationships.
The best solution would be when you create a third table (contacts2sites) in this table you have three columns called: id, siteid, contactid.
In this table you can add every connection between sites and contacts. To query the data out of it you can use the mysql query with joins over all tables.
little example:
solution with joins
DETAILS
I have a quiz (let’s call it quiz1). Quiz1 uses the same wordlist each time it is generated.
If the user needs to, they can skip words to complete the quiz. I’d like to store those skipped words in mysql and then later perform statistics on them.
At first I was going to store the missed words in one column as a string. Each word would be separated by a comma.
|testid | missedwords | score | userid |
*************************************************************************
| quiz1 | wordlist,missed,skipped,words | 59 | 1 |
| quiz2 | different,quiz,list | 65 | 1 |
The problem with this approach is that I want to show statistics at the end of each quiz about which words were most frequently missed by users who took quiz1.
I’m assuming that storing missed words in one column as above is inefficient for this purpose as I'd need to extract the information and then tally it -(probably tally using php- unless I stored that tallied data in a separate table).
I then thought perhaps I need to create a separate table for the missed words
The advantage of the below table is that it should be easy to tally the words from the table below.
|Instance| missed word |
*****************************
| 1 | wordlist |
| 1 | missed |
| 1 | skipped |
Another approach
I could create a table with tallys and update it each time quiz1 was taken.
Testid | wordlist| missed| skipped| otherword|
**************************************************
Quiz1 | 1 | 1| 1| 0 |
The problem with this approach is that I would need a different table for each quiz, because each quiz will use different words. Also information is lost because only the tally is kept not the related data such which user missed which words.
Question
Which approach would you use? Why? Alternative approaches to this task are welcome. If you see any flaws in my logic please feel free to point them out.
EDIT
Users will be able to retake the quiz as many times as they like. Their information will not be updated, instead a new instance would be created for each quiz they retook.
The best way to do this is to have the word collection completely normalized. This way, analyses will be easy and fast.
quiz_words with wordID, word
quiz_skipped_words with quizID, userID, wordID
To get all the skipped words of a user:
SELECT wordID, word
FROM quiz_words
JOIN quiz_skipped_words USING (wordID)
WHERE userID = ?;
You could add a group by clause to have group counts of the same word.
To get the count of a specific word:
SELECT COUNT(*)
FROM quiz_words
WHERE word LIKE '?';
According to database normalization theory, second approach is better, because ideally one relational table cell should store only one value, which is atomic and unsplitable. Each word is an entity instance.
Also, I might suggest to not create Quiz-Word tables, but reserve another column in Missed-Word table for quiz, for which this word was specified, then use this column as a foreign key for Quiz table. Then you probably may avoid real time table generation (which is a "bad practice" in database design).
why not have a quiz table and quiz_words table, the quiz_words table would store id,quizID,word as columns. Then for each quiz instance create records in the quiz_words table for each word the user did use.
You could then run mysql counts on the quiz_words table based on quizID and or quiz type
The best solution (from my pov) for what are you trying to achieve is the normalized aproach:
test table which has test_id column and other columns
missed_words table which has id (AI PK) and word (UQ) , here you can also have a hits column that should be incremented each time that a association to this word is made in test_missed_words table this way you have the stats that you want already compiled and you don't need them to be calculated from a select query
test_missed_words which is a link table that has test_id and missed_word_id (composite PK)
This way you do not have redundant data (missed words) and you can extract easily that stats that you want
Keeping as much information as possible (and being able to compile user-specific stats later as well as overall stats now) I would create a table structure similar to:
Stats
quizId | userId | type| wordId|
******************************************
1 | 1 | missed| 4|
1 | 1 | skipped| 7|
Where type can either be an int defining the different types of actions, or a string representation - depending on if you believe it can ever be more. ^^
Then:
Quizzes
quizId | quizName|
********************
1| Quiz 1|
With the word list made for each quiz like:
WordList (pk: wordId)
quizId | wordId| word|
***************************
1 | 1 | Cat|
1 | 2 | Dog|
You would have your user table however you want, we are just linking the id from it in to this system.
With this, all id fields will be non-unique keys in the stats table. When a user skips or misses a word, you would add the id of that word to the stats table along with relevant quizId and type. Getting stats this way would make it easy as a per-user basis, a per-word basis, or a per-type basis - or a combination of the three. It will also make the word list for each quiz easily available as well for making the quizzes. ^^
Hope this helps!