Php 从表单插入时连接2个变量

Php 从表单插入时连接2个变量,php,mysqli,Php,Mysqli,不知道该怎么弄清楚!我正在尝试创建一个班级评分图。我有一个4列4行的表。班级水平,学生垂直 列由db生成,如下所示: <form action="insert.php" method="post"> <table> <thead> <tr> <th>Name</th> <? $query = "SELECT classname FROM class ORDER B

不知道该怎么弄清楚!我正在尝试创建一个班级评分图。我有一个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>

名称
现在,这是我如何使sudent垂直的:

<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连接在一起,以便在插入db时可以知道每个学生的班级和成绩

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

好问题

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

这种链接数据的方式称为关系数据库:

此字段设置称为多对多关系:

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

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