如何分配一个学生学习php和mysql的多个课程?

l7wslrjt  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(289)

我正在学习php,我们昨天有一个项目。我需要建立一个管理学校的系统。我需要创建课程和学生,我需要能够分配一个学生到多个课程。我需要能够显示每个课程的学生名单,如果我选择了一个学生,我需要显示他列出的所有课程。我的问题是如何正确规划数据库,以便能够显示所有这些数据。我想我需要在每门课程中使用一个学生id的数组,但我不知道在哪里和如何开始(我们没有学习它。。。就像许多其他事情一样……)。
我们将不胜感激。
新内容:
我已经创建了3个表。1学生2。课程3。coursestudent(有:-id-studentid-courseid)
我需要帮助写一个查询,将计数有多少是每个课程列出。
这是我的密码:

<table id="courses" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
                <th class="text-center">#</th>
                <th class="text-center">שם הקורס</th>
                <th class="text-center">כמות סטודנטים בקורס</th>

            </tr>
        </thead>
        <tbody>
    <?php
    $counter=1;
    $sql = "SELECT * FROM courses";
    $result = mysqli_query($conn, $sql);

    while($row = mysqli_fetch_assoc($result)){ 
        echo '
            <tr class="text-center">
                <td>'. $counter .'</td>
                <td>'. $row['coursename'] .'</td>
                <td>';
                $sqlstudents = "SELECT * FROM coursestudent INNER JOIN students ON coursestudent.studentid=students.studentid WHERE courseid='$counter'";
                $resultstudents = mysqli_query($conn, $sqlstudents);
                $rowcount=mysqli_num_rows($resultstudents);
                echo  $rowcount.'</td><tr>';
            $counter++;  
    }           
        ?>       
        </tbody>
    </table>

每门课程返回0。

dgsult0t

dgsult0t1#

你需要三张table。一个用于存储关于学生的信息,一个用于类,一个连接表用于存储哪个学生被分配到哪个类-基本上是多对多关系。这件衣服可能有用。

ui7jx7zq

ui7jx7zq2#

这个地方不适合问这种问题。因为我有一些知识,但我仍然给你解释怎么做。
从你的问题来看: My question is how to plan the DB correctly in order to be able to show all this data. 以下是如何根据您的需求规划数据库:

1) You have to create 3 tables mainly one will be students where you are going to 
   enter student information.

2)Another one will be courses table where you have all course information.

3)And another table will be Student_courseslist table where you can assign courses to 
   students and it will show you the  list of students assigned to that particular 
  course.

相关问题