【问题标题】:How to assign a student to multiple courses in php and mysql?如何将学生分配到 php 和 mysql 中的多个课程?
【发布时间】:2018-08-01 10:34:17
【问题描述】:

我正在学习 PHP,我们昨天有一个项目。 我需要建立一个管理学校的系统。 我需要创建课程和学生,并且我需要能够将学生分配到多个课程。 我需要能够显示每门课程的学生列表,如果我选择一个学生,我需要显示他列出的所有课程。 我的问题是如何正确规划数据库以便能够显示所有这些数据。 我想我需要在每门课程中使用一组学生 ID,但我不知道从哪里开始以及如何开始(我们没有学习它......就像许多其他东西一样......)。

我们将不胜感激。


新内容:

我创建了 3 个表。 1. 学生 2. 课程 3. 课程学生(其中有: - ID - 学生卡 -课程ID )

我需要帮助编写一个查询,以计算每门课程列出了多少。

这是我的代码:

<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。

【问题讨论】:

  • 这里不适合这类问题。人们希望在格式良好的问题中看到您尝试过的内容(您的代码)。帮助他们帮助你。请拨打stack overflow tour了解更多关于本网站的信息,并阅读how to ask
  • StackOverflow 并不是真正适用于这样的问题。但我会试着把你推向正确的方向。您正在寻找的是多对多关系(一个学生可以有多个课程,一个课程可以有多个学生)。为此,您可以使用连接表。基本上,您将有 1 个学生表,1 个课程表和第三个将学生 ID 链接到课程 ID 的表。
  • 阅读 MySQL 中的规范化stackoverflow.com/a/1258776/2469308

标签: php mysql arrays categories


【解决方案1】:

您将需要三张桌子。一个存储学生信息,一个存储班级信息,一个连接表存储哪个学生被分配到哪个班级 - 基本上是多对多关系。 This answear 可能会有所帮助。

【讨论】:

    【解决方案2】:

    这里不适合提出这类问题。由于我有一些知识,但我仍在向您解释如何做。

    根据您的问题: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.
    

    【讨论】:

    • 谢谢。已经做了。现在我正在尝试编写一个正确的查询来显示每门课程列出的学生人数。
    猜你喜欢
    • 2021-11-28
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多