I've heard that creating a session and assigning a session ID to each user is the way to go for a case like this. However, I'm still a little unsure- if the user closes their browser or clears their cookies then the session is deleted from what I understand. And even then, sessions are temporary so the session will be deleted regardless. Doesn't that mean that whenever the session ends- the users data is wiped? And therefore if they wanted to get their data back they'd have to create a brand new account and the program would have to interpret their new data all over again only to be deleted again later? I apologize for my lack of understanding- I'm fairly new to this. How would I make it so that my website retains the data unique to each user permanently? And am I misunderstanding the concept of sessions in PHP?This image will show the design structure of my two tables. I've attempted to reference one to the other to no avail unfortunately. The green link between them represents the reference of the idUsers column in the "users" table (user account information), and the userIDs as the foreign key, a column in the "user_classes" table.
Asked
Active
Viewed 86 times
0
-
If you are referring to a method of persisting a login, you might find [“Keep Me Logged In” - the best approach](https://stackoverflow.com/questions/1354999/keep-me-logged-in-the-best-approach) helpful. Also see [The definitive guide to form-based website authentication](https://stackoverflow.com/questions/549/the-definitive-guide-to-form-based-website-authentication/477578#477578). – showdev Jul 16 '19 at 03:45
-
Well I don't really mean keeping the user logged in. I just mean what's the most effective way at storing user information that's unique to each individual user, which I want to be retained and not wiped- being accessible to the user immediately once they login (provided they gave the correct login information of course). – DouglasstheDog Jul 16 '19 at 03:49
-
1I assume you're storing unique user information in a database. When a user logs in, that data is available to your application. It's never wiped unless you delete a users data from the database. – showdev Jul 16 '19 at 03:51
-
Ah ok. Well I'm using PHPmyadmin. But I thought I had to use sessions in order to connect the data with a particular user and assign them a unique variable. If that's not the case, do you know how I would connect data to the specific user who inputted it in order to give different results (depending on what the user did)? – DouglasstheDog Jul 16 '19 at 04:00
-
To clarify, for example I have two different tables right now. One that stores account information when the user signs up (username, password, email, etc.) and another table that stores whatever the user inputted in these text fields called "classes". However, how would I make my program/database realize which classes belong to what user? – DouglasstheDog Jul 16 '19 at 04:07
-
To form a relationship between two tables, you can store a user's ID (usually a [auto-incremented integer](https://stackoverflow.com/questions/5665571/auto-increment-in-phpmyadmin)) in a column of the "classes" table. That way you can [join](https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins/21085546) the "classes" rows to "users" rows based on the users ID. Some examples can be found [here](https://stackoverflow.com/questions/655446/what-exactly-is-a-foreign-key). – showdev Jul 16 '19 at 04:10
-
Check out this [sample autoshop database](http://sqlfiddle.com/#!9/faf2f/1). Notice how each row in the "Cars" table references a "CustomerId" from the "Customers" table and an "EmployeeId" from the "Employees" table. Thereby each car is related to a particular customer and a particular employee. In the same way, you can relate your "classes" to particular "users". – showdev Jul 16 '19 at 04:19
-
So to confirm- I'd reference the userID from the user account table into a new column inside the classes table? And I could do that for every other table that contains specific data unique to each user and the userID would remain the same correct? So like if my auto-incremented integer in the userID column in the "user account" table is 10 let's say, and I've referenced the two tables- the userID should correspond to the appropriate user in the "classes" table correct? In other words: userID 10 will correspond to the exact same user in other tables, not just within the user account table? – DouglasstheDog Jul 16 '19 at 04:22
-
That's right. The `userID` would be a unique ID number for one specific user. If you store that same value in a "classes" row, then it forms a relationship between that "classes" row and the specific user with that `userID`. You can `JOIN` the "classes" table to the "users" table `ON` that `userID` in order to (for example) fetch all the classes for one specific user. If you like, it might help to include your table structure in your question. – showdev Jul 16 '19 at 04:26
-
Yes, I edited my paragraph and linked an image to show the structure and relationship between the two tables. – DouglasstheDog Jul 16 '19 at 04:36
-
Thank you. A couple of questions: Can a single row of the "classes" table be assigned to multiple users, or does each "classes" row pertain to one user? What is the difference between `idUsers` and `uidUsers` and what do they each represent? – showdev Jul 16 '19 at 04:42
-
Right so- idUsers is the auto-incremented int of the user. uidUsers is the actual username the user signed up with "DouglasstheDog" for example. Class1, class2, etc. represent text fields. There is no required amount of classes the users must type into(max 8), and they can type into any single one of them. If multiple users typed "Math" for class1, then in that regard I guess they could be assigned to multiple users. – DouglasstheDog Jul 16 '19 at 04:47
-
Multiple users adding the same class describes a "many-to-many" relationship (i.e. many users can have any one class, and any one user can have many classes). In that situation, you might consider a third table to hold the relationships. That table would have a column for `userID` and a column for `classID`. For example, one row might relate user #1 with class #1; another row might relate user #1 with class #2; and another row might relate user #2 with class #1, etc. See [one-to-one, one-to-many and many-to-many relationships](https://stackoverflow.com/q/7296846/924299). – showdev Jul 16 '19 at 04:53
-
But the program would still recognize uniqueness correct? So like- if User #1 typed "Math" for class1, and User #2 typed "Science" for class1- then class1 would appear different for both because it'd have an assigned classID right? Also- don't the classes within the second table act as the defacto classIDs? – DouglasstheDog Jul 16 '19 at 05:07
-
In a typical "many-to-many" relationship, there would be only one "classes" entry for "Math"; let's say it has `classID` 1. You could assign that `classID` to any number of users. If you're adding "Math" to the "classes" table multiple times, then the data is unnecessarily redundant. When a user types in a class, your application would search to see if that class already exists in the classes table and then add a record containing that `classID` and the user's `userID` to the third junction table. If the class doesn't already exist, it would add a new class and use that new `classID`. – showdev Jul 16 '19 at 05:13
-
Yes, whichever table has the auto-increment for `classID` defines the unique IDs. In your case, that's the "classes" table. Other tables simply reference those class IDs. – showdev Jul 16 '19 at 05:13
-
And lastly, final two questions: one row could still relate user #1 to multiple classes correct? So like user #1 to class1, class2, class3, and user #2 to class1, class2, class3, class4, class5 for example? Lastly- would I reference only the user IDs? And would the structure of the referencing be: table 1 to table 2 to table 3? – DouglasstheDog Jul 16 '19 at 05:15
-
Ah ok- I think I understand now. So let's say everything is up and running. A user types "Math", and suddenly "Math" is given a unique class ID automatically within the database and any users who type "Math" will receive that ID in their classes column? And so if a user even types something random like "lol" then that as well is automatically given a unique class ID? But then if someone types Math in the class1 text field, and someone else types Math in the class2 text field- does the database recognize those as two separate IDs or does it just see Math as Math and therefore under the same ID? – DouglasstheDog Jul 16 '19 at 05:20
-
1) Yeah, exactly. Except that you'll need to code the part that looks to see whether an entered class already exists. One idea is to set the class name to `unique` and use [`ON DUPLICATE KEY UPDATE`](https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html) to prevent duplicates; then you can return the proper `classID` regardless of whether it was inserted or already exists. 2) The same `classID` would be used for "Math" everywhere, since there's only one entry in "classes" for "Math". Here's an [example of how it might work](http://sqlfiddle.com/#!9/170b4d/2). – showdev Jul 16 '19 at 05:39
-
I apologize for the long discussion- I just wanted to clarify absolutely everything haha. Thank you so much for your unparalleled help and sticking by me for this long! – DouglasstheDog Jul 16 '19 at 05:45
-
No worries! A chat might have been easier, but I didn't see the option for it. Best of luck! – showdev Jul 16 '19 at 05:45