Dealing with permissions with MYSQL and PHP [duplicate] - php

This question already has answers here:
Many-to-many relationships examples
(5 answers)
Closed last year.
I'm doing a project for a music school and I have a table for students, a table for courses available and I was creating the table for teachers. My question is, how can i manage to "link" a teacher to a certain or more than one course, so that when the teacher logs in the admin page, he can only update and add information to the students that belong to the course/s that he is teaching.
My first thought was that I should have a field in the teachers table that could only contain values from the courseID (for example, if i want to register a guitar teacher and the ID of the guitar course is 3, i should have a field in the teachers table named teacher_course_id which would have the value 3).I also thought about having more than one teacher for a course, and if so, how can I give permissions to one teacher only (the teacher of that student) and not all the teachers from that especific course?
I don't really need the code for anything, I just wanted a a brief explanation on how could I do it. If I wasn't clear enough I can give more details in the comments

You need one more table for relation between teachers and courses, smth like that:
course
id
title
...
teacher
id
name
...
teacher_course
course_id
teacher_id
can_edit 0/1
...

You can create table with fields teacher_id, course_id with Primary Key (teacher_id, course_id)

Related

Database/PHP design- adding a list of other entity instances to another entity instance?

So my question is very much just a database design question. I'm relatively new to PHP, taking my first database course, and I'm trying to figure out how best to execute my idea.
So I'm building a membership database. Within this database there are "members" and there are "meetings," represented as two separate tables. I'm wondering what might be the best way to add a list of members to a meeting instance, or create a relationship table between the two. For example, would you advise that each member ID (primary key) be added individually (say, via a bunch of text input form fields) when creating a new meeting instance? Or perhaps is there a way to easily have the user upload a CSV or excel file of primary key user id numbers and, from those user number ids, easily create a relationship table?
Hope this is clear- just hoping to get some advice/insight, perhaps I'm not aware of the easiest way... Thanks!
I don't know what are you trying to do in your particular case, but is sounds to me that you should have three tables:
members - you have that one already
meetings - you also have that one already
members_meetings: this one is the table, that will join the two tables. And the required fields in that table should be:
member_id - the id of particular member, points to the id field in your members table
meeting_id - the id of the meeting, this member is attending, points to the id field in the meetings table
Than, if you want to get all members, that are attending meating X, you can just run the following query:
SELECT members.* FROM members_meetings LEFT JOIN members ON members_meetings.member_id = members.id WHERE members_meetings.meeting_id=X

Best way to store exam data in a database [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am looking to implement a new database and store students exam data and marks in the database. There are an arbitrary number of students and each student has an arbitrary number of modules with corresponding marks. I need the database to be created so that I am able to query the database and return the students name followed by each of their modules and corresponding marks and then to move onto the next student. This is because I want to display it in PHP with a list of students names in bold and then a table of their modules and their marks.
I initially considered to organise the table like this:
| StudentName | Module | Result |
With a new entry for each module that the student takes and just store multiple students but then I do not know how I would then query the database and retrieve each StudentName individually and then be able to loop through their corresponding modules and results to store it in an HTML table.
Any ideas would be appreciated
Give each table an id field that auto increments
Where you need to link results to modules, for example, you'd add a
moduleID field to the result table to tie the result to the module.
You'd then do queries with joins to bring in both sets of data.
To tie students to modules, you'd need another table that has a
studentID field and a ModuleID field. You'd then query this table
and join on StudentName and Module to get the respective data for
each.
First, you need several tables. One table cannot do it. Create a table for each type of objects. In this case, you need a table for students and one for modules.
Both tables need an id column to identify the rows. Create a primary key on them.
Students table: Id, StudentName
Module table: Id, ModuleName
There is a many to many relationship between students and modules (each student can have more than one module and each module can be chosen by many students). A many to many relationship requires a separate table.
You were not clear if there is one grade per student and module. If there is only one grade, the best solution is to include the grade in this relationship table.
Grade table: Id, StudentId, ModuleId, Grade

Inserting into a Table joined to another Table by a reference table?

I came up with this database design after discussing it with many people over IRC.
In my project, a User aka Member can have many "Teams" & "Projects" on his account and can be part of some "Projects" as well. Moreover, "Teams" can have many "Members" as well including the Member who is creating it.
Now my question is, suppose, a Member wants to create a Project or a Team under his account (later on I want to see all projects created by a specific member), can I do a insert into the using following in PHP?
INSERT INTO projects VALUES(values) WHERE member_id = something
I can get the member_id using the session variable I guess.
Use regular INSERT to create the projects or teams row, then use the ID created there to insert into the projects_members or teams_members. Also settle on plural or singular names for your tables (currently you have team not teams).
Your query would be somethings like this
Insert into
table(columns)
select (columns) from
another_table
where
conditions=.
Just Remember One thing the order and names of columns must match.
If Member wants to create a Project or a Team under his account it will not be possible using current structure.
You need to add one more column created_by on projects and team tables.
The name of "team" table should be "teams".

Querying multiple tables MySQL

I use MySQL and trying to write a PHP script for my school project.
There is one table named lessons contains this columns:
-id
-lessonid.
-studentid
I also have two different tables for notes and announcements
announcements and notes tables contains these columns:
-id
-lessonid
-content
-createdtime
I need to order both announcements and notes from latest to oldest by createdtime but also need to show all lessons a student takes.
For example: A students takes maths and physics lessons. I need to display him/her both notes and announcements for both of physics and maths and all items should be ordered by date. (like a timeline.) And of course I will not show him/her the notes and announcements for chemistry lesson. Also it will be good if I can say it is note or announcement on the list.
Can you help me to write SQL and PHP code for that?
Thanks.
EDIT: This is where I have stuck:
I have combined two tables and ordered them by date. But can't combine them with the lessons a student take.
SELECT title, created, lessonid FROM (SELECT title, created, lessonid FROM notes UNION SELECT title, created, lessonid FROM announcements) as a ORDER BY created DESC
First of all, thanks for letting us know that this is for a school project - therefore I won't give you the answer. If it is in the project then your teacher should have given you the concepts to come up with a solution.
Your question is well put together and I can see how to solve it but ... It's your project so you need to have a crack at it and post what you come up with.
I will give you some hints to get you started.
You need a query to combine the announcements and notes table. Then you need to group the data by the lesson and join that to the students. This is all basic SQL.
Good luck. Post what you come up with.
I'll also, follow fellow posters advice, and not do the legwork for you. but won't let you go empty handed, so will give you the concept.
there is a thing called third normal form, we decide how many tables according to that concept, so if its a big database then separate table for first name and separate for last name, as many people share those among themselves, so saves space and redundancy etc. so one table for person has personid as primary, and has lastname foreign key to refer to last name table , we generally name it lastNameRef, similarly firstNameRef. so now, each person has lot of classes, and each class has lot of persons(students) in it. so this is a many-many relation - we create a allreference table to solve this many to many problem. so there is one table for classes which has class id as primary key, so now u create a all reference table which a recordId as primarykey, (just for namesake) and personRef(refers to personId in person table) and classref(refers to classId in class table) if one person has two classes, another entry with same personId but different class Id, at the end, you can query the name of person from person table, and name of class from class table and create join on their foreign keys but use all three tables, result is (JOHN MATH, JOHN SCIENCE) etc, same way you display all notes for john searching name in person table, and subject in class table,and notes in notes table

How To Save the State of Current Work?

I am working on a site that has tests users can take. I have a test area set up with 100 questions on each subject. If a user is taking a test, I want them to be able to save the work they have already done and when they return I want them to be able to continue the work from where they left. The questions being answer are multiple choice questions and they answer them by selecting the radio button and each radio button has a value of A, B, C, D, or E. I am using PHP and MySQL for my programming and database storage.
Would I need a table for each test and have 100 column names such as 1,2,3,4,5 all the way up to one hundred and when the user saves their work it will store the questions they have completed in the database? Is this a good method or is their another way of doing this?
Have a table for 'users' and a table for 'questions'.
Have another table, 'answers', with a foreign key to 'questions', a foreign key to 'users' and a column called 'answer'.
When the user answers a question, insert a record into the 'answers' table with the user id, the question id and the answer they provided.
If they have not yet answered the question, then there will not be a record in this table.
This is what I've come up with for a first Draft.
User
--------
UserId
UserName
Test
-------
TestId
TestName
TestQuestions
----------------
QuestionId
TestId
QuestionName
QuestionChoices
----------------
ChoiceId
QuestionId
Choice
UserTestAnswers
-----------
UserId
ChoiceId

Categories