0

不知道如何解决这个问题!我正在尝试创建一个学校班级评分图。我有一个有 4 列和 4 行的表。班级水平和学生垂直。

这些列是从 db 生成的,如下所示:

<form action="insert.php" method="post">
<table>
<thead>
    <tr>
        <th>Name</th>
        <?
        $query = "SELECT classname FROM class ORDER BY cname LIMIT 3";

        if ($result = $mysqli->query($query)) {
            while ($row = $result->fetch_assoc()) {
                $classid = $row['classid'];
                echo '<th><div class="verticalText">'.$row['classname'].'</div></th>';
            }
        }
       ?> 
    </tr>
</thead>

现在,这就是我获得垂直方向的方式:

<tbody>
<?
$query = "SELECT studentid, studentname FROM students ORDER BY studentname";

if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {

        $studentid = $row['studentid'];

        echo '<tr>';
        echo '<td style="text-align:left;"><a href="#">'.$row["studentname"].'</a></td>';
        echo '<td>';
        echo '<input type="text" name="grade" id="grade" placeholder="--">';
        echo '</td>';   
        echo '</tr>';
    }

    /* free result set */
    $result->free();
}

/* close connection */
$mysqli->close();
?>  
</tbody>
</table>
<input type="submit" value="report" name="submit">
</form>

我的问题是。我如何在我的 insert.php 上将 classid 和 studentid 连接在一起,这样当我插入数据库时​​,我可以知道每个学生的班级和成绩?

希望这有任何意义,并期待任何答案。

4

1 回答 1

0

好问题!

首先,您的班级表将需要自动递增 id,以便您可以获得对班级行的干净引用(基于名称的引用被认为是不好的做法),之后您需要添加一个包含 3 列的第三张表:class_id、student_id 和grade . 如果您愿意,可以在 student_id 和 class_id 上添加组合主键,这样组合就必须是唯一的。(我认为这就是你想要的)

这种链接数据的方式称为关系数据库:https ://en.wikipedia.org/wiki/Relational_database

并且此字段设置称为多对多关系:http://en.wikipedia.org/wiki/Many-to-many_(data_model)

// So your relational table will look like:
- student_id (part of primary key and foreign key that points to the student)
- class_id (part of primary key and foreign key that points to the class)
- grade

这里有很多东西要学,看看文章就去试试。祝你好运!:)

于 2013-05-27T22:36:17.360 回答