Storing multiple checklist data in MySQL using PHP - php

i have a table which consists of student details like roll no, Name, age. Now i have a table for storing the students who have attended and not attended on the given day and given class. I will be using a form with checklist and names automatically displayed using the student details table, i.e. All the name name and roll no will get displayed with a checkbox to each name. If it checked, it mean the particular student has attended the class and if not checked, then the student didn't attended. These detail should get update in the attendance table. How to do it using php and MySQL?

Use a many to many relation in MySQL.
You have a database table with students, like:
Table classes
Student (primary)
StudentName
StudentAge
(further info)
Create a database table for the Classes with they can attend, like:
Table classes
ClassID (primary)
ClassName
ClassDate
(further info)
Then create a many to many table, like:
Table attendances
ClassID (primary)
StudentID (primary)
To get the students that attended the class do the following query:
<?php
$lstrQuery = "SELECT `students`.* FROM `students`
INNER JOIN `attendances` ON `attendances`.`StudentID` = `students`.`StudentID`
AND `attendances`.`ClassID` = {YOUR_CLASS_ID} ";
$loResult = mysql_query($lstrQuery);
while($laRow = mysql_fetch_assoc($loResult))
{
// do whatever you like
}
?>
Is this what you had in mind?

Related

Adding foreign key dynamically in a database via php

Ok I am new in php development so I did some mistakes, didnt planned my database just started because I was so excited about my first project, SO dont judg me.. :D :D
this is my Primary database
Primary Database
and this is my Movies database,
Movies database
so I created the songs database first and stored the data in it, after that i created the movies database and movie_id is the foreign key of the id of movies database, so there are too many songs to add foreign keys in primary database, I want a php script which can insert the foreign key for me in my primary database,
I created a php script ( actually tried for several hours) but didnt succseeded
I wanted to
get movie_name from songs database
match with the movie_name of movies databse
if both movies matches
insert the id of movies database into the movie_id(foreign key) of song database
<?php
require_once ('../inc/db.php') ?>
<?php
$lang_query = " SELECT * FROM songs";
$query = "UPDATE songs SET movie_id = '$mov_id'";
$lang_run = mysqli_query($conn, $lang_query);
$mov_query = " SELECT * FROM movies";
$mov_run = mysqli_query($conn, $mov_query);
$mov_row = 1;
$lang_row = 1;
while ($mov_row = mysqli_fetch_array($mov_run))
{
$mov_name = $mov_row['movie_name'];
$mov_id = $mov_row['id'];
while ($lang_row = mysqli_fetch_array($lang_run))
{
echo $movie_name = $lang_row['movie_name'];
$movie_id = $lang_row['id'];
if ($movie_name == $mov_name)
{
mysqli_query($conn, "UPDATE songs SET movie_id = '$mov_id' where id = '$movie_id'");
}
}
} ?>
Please Help me, Thanks :)
I think you can do it by running following sql query -
update *songs* inner join *movies* on songs.movie_name=movies.movie_name set songs.movie_id=movie.id
I honestly think that before you worry about trying to add foreign keys based on your current schema, that you really need to revisit the schema and normalize the data. Why have the same movie name and movie slug fields in two tables? Why have movies, songs, singers, actors, categories, etc. all in the same table? These things probably all need their own tables that are related to each other.
When building your database, think in real world terms, because you are likely going to want your application users to be able to interact with the data in the database in a real world sense. To me, you would probably need the following tables at a minimum:
movies
songs
movies_to_songs (join table to express many-to-many relationship)
actors
movies_to_actors (many-to-many)
editors
movies_to_editors (many-to-many)
movie_categories
movies_to_movie_categories (if you want to treat this as many-to-many)
singers
songs_to_singers (many-to-many)
youtube_videos (a separate table where you could store all video data)
Each table would have additional columns (properties) that are specific only to a single entity of the type contained in the table. So, for example a movie table might look like
id (primary key)
name
slug
image
language
release_date
youtube_id (reference to listing on youtube_videos table)
And you might have an actors table like:
id (primary key)
name
... (sex, birthday, etc.)
And a movies_to_actors table that is just two columns with compound primary key (i.e. combinations must be unique)
movie_id (references primary key id in movie table)
actor_id (reference primary key id in actors table)
And so on across your various tables.
Just remember to think about the real world relation of one object to another and the real world properties (columns) for each of those individual objects.

how to retrieve data from sql table and insert it into another table

I am developing a 'online students discussion platform' for a certain university, i want a student to post a question to only students belonging in the same faculty so that if ICT student posts a question, the system extracts all the students in ICT faculty from the students table, attaches them to the post and inserts them into the table receivers.
The system is extracting all students from the students table but it is inserting only one student in the table receivers.
TABLES:
-students(regno,name,faculty_code) PRIMARY KEY regn
-receivers(id, regno,message_id)
how to do it?
Instead of having a table for receivers, a better approach would be for you to have a "faculty_code" field in the "message" table which would be populated with the faculty_code of the poster. So for each student, extract messages where student.faculty_code is equal to message.faculty_code.
That way a student only sees messages posted by students in the same faculty as him/her.

php mysql relational definition in tables

guys I've been trying to create create a school results management system.There is a STUDENT table and many COURSE tables. Each student must be linked to all Courses tables. e.g JOHN, MARY,PETER must partake in MATHEMATICS, ENGLISH, ELECTROPHYSICS, PROGRAMMING etc
My problem is how to define this relationship in mysql and at same time let these students fetch only their own results in all the courses.
I don't know if you have a requirement to have each course in a separate COURSE table but that doesn't make much sense. You should have a STUDENT table and a COURSES table. Then, you will need a STUDENT_COURSES table to show which courses a student has taken or must take. Your table structure should be something like:
STUDENT: Id, Name, Gender, etc
STUDENT_COURSES: Id, Student_Id, Course_Id, Taken, Grade
COURSES: Id, Course_Name
When a new student is created, you would need to add records to the STUDENT_COURSES table to insert the new STUDENT.Id into the Student_Id field and the Course_Id for each of the required courses. The default value for Taken would be false and the Grade would be Null or Empty String.
This is a very simplistic example and would not work in a real world solution because you would need to have Instructors, Locations, Etc. to make anything useful. But hopefully this will help you get started.

Resolving table(mysql, php)

I got 3 table, called class , student and class_student(resolving table) . This is due to many to many relationship between class and student. A student can have many class and a class can have many student. The fields inside class is(class_id, class_start_time and class_end_time) , student(student_id, student_name, student_age) , class_student(student_id , class_id). My question would be : Inside a class, it includes student , how do I add student into the class table ? Or should I make use of the resolving table? My understanding of resolving table is weak and I'm not sure what's the purpose of it.
Thank you all for your answer !
What about when I add a new record to class student table , do I add a new record to class table as well?
The most common way to resolve a many to many relationship is via a seperate relation table (as Barmar noted).
In your case that table could have these fields:
table class_students
--------------------
id // an unique id of that relationship;
// could be ommitted, could be an autoincrement,
// could also be a "handcrafted" id like classid_userid -> 21_13 (I need this kind of id's for an ember-api.
// All depending on your needs
class // the id of the related class
student // the id of the related student
// maybe add additional fields:
type // to describe that relationship
sort
You then would get all the students of a specific class like so:
$class_id = 1;
$sql = "Select * from students, class_students where students.id=class_students.student AND class_student.class=".$class_id;
// note, that you should do that via prepared statements,
// this is only for simplicity to show how to proceed.
You are not going to add students in the class table. If you do so, there is no point separating class from student in the first( you have prevented redundancy of data ). I believe the table structure is good enough to achieve your objectives. The secondary keys (student_id , class_id) which are primary key in the student and the class tables respectively does the job.
class table
id| title | start | end
1 Biology 8am 10am
2 English 10am 12pm
student table
id | name |
1 John
2 Doe
student_class table
student_id | class_id
1 1
1 2
2 1
from the table, i could
Get all courses John( user.id: 1 ) registered for
SELECT FROM student_class WHERE student.student_id = '1'
All the students that registered for Biology (class.id: 1)
SELECT FROM student_class WHERE student.class_id = '1'
Note I know you will need the name of the student here in the result.
Very simply , just 'LEFT JOIN' class table with student_class table on student_class.class_id = student.id
Then for each record you get from student_class table, the 'name' (or other columns you choose to include in the result set) will be added from the class table.
Note the way you just joined the class table, you can do the same for student table. for example, You want the student to print their time table for classed they registered for, you will still select from student_class table and get the start and the end time using LEFT JOIN

How can I make this database schema better?

I currently have two tables in which stores the attendances of a student in a course. I have the hub_attendance table which stores the total attendances of a student and the hub_attendance_lesson where it stores the attendance of each lesson that a student has or has not attended. I'm not sure if this is correct or if I'm doing anything wrong, I'm a beginner in databases!
hub_attendance:
id
student_id
course_id
total_lessons
total_lessons_so_far
total_attended
total_absent
total_excused_absent
total_late
total_excused_late
hub_attendance_lesson:
id
lesson_id
course_id
student_id
date
attended
absent
excused_absent
late
excused_late
EDIT:
So I've gotten rid of the first table completely and this is my new single table.
Hub_Attendance:
id
lesson_id
course_id
student_id
date
attendance
As Dutchie432 said, you don't need the first table because it introduces unnecessary redundancy and you can count those statistics on the fly. Such aggregate tables can be a good solution if performance is an issue, but they should be used only as a last resort.
About the second table - you have separate fields attended, absent, excused_absent,
late and excused_late. Aren't these mutually exclusive? So only one of them can be true for one row? If so, you may be better off with one enumeration field called for example attendance, which would take different values for each of those states. In that way you could't have rows where none of the flags, or more than one flag, is set.
Here's what you need:
**Course**
id, name, etc...
**Lesson**
id, courseid, name, etc...
**Attendance**
id, studentid, lessonid, lateness, etc...
**Enrolment**
id, courseid, studentid, startdate, etc...
You need the enrolment table to know that students should be on a course even if they never turn up for lessons. The attendance table will allow you to have many students per lesson and many lessons per student. This is a many-to-many table. Any aggregation and counting can be done in SQL.
If I understand your schema correctly, Your first table can be totally eliminated. You should be able to fetch the totals using MySQL.
select count(id) as total_late from hub_attendance_lesson where late=true and student_id=TheUserId
Remove this first table, every total can be fetch using SQL :
SELECT count(id) AS absent
FROM hub_attendance_lesson
WHERE lesson_id = <your lesson id>
AND absent = <false / true>
I guess you'll be able to adapt this code for your needs.

Categories