MySQLli 连接两个表以将用户名与相应的用户 ID 匹配


mysqli join two tables to match user name with the corresponding user id

我很难弄清楚这一点。

我有两个表"teacher_info"和"section_info"。

在我的

表单中,我在我的section_info表中包含了所有属性,除了我使用教师姓名而不是教师 ID 以便于选择,这是教师姓名的下拉列表,这是我的代码

<?php
include("anhsis.php");
mysqli_select_db($con,"anhsis");
$result= mysqli_query($con,"SELECT t_lname,t_fname FROM teacher_info");
echo"<select name='adviser' class='form-control' required>";
echo"<option value='0'>--Select Adviser--</option>";
while ($row=mysqli_fetch_array($result)) {
echo "<option value='".$row['t_lname']."".$row['t_fname']."'>".$row['t_lname'].", ".$row['t_fname']."</option>";
}
echo'</select>'
?>         

这是我在"section_info"中插入数据的 PHP 代码

<?php
        include_once('anhsis.php');
        $room_id = $_POST['room_id'];
        $section = $_POST['section'];
        $adviser = $_POST['teacher_id'];
        $level = $_POST['level'];
        $curriculum = $_POST['curriculum'];
mysqli_select_db($con,"anhsis");
    $result= mysqli_query($con,"SELECT * FROM section_info WHERE room_id= '$room_id'");
    if (mysqli_num_rows($result)>0){
        echo '<script type="text/javascript">';
        echo 'alert("TIN No already exist!")';
        echo '</script>';
    }
else{
    mysqli_query($con,"INSERT INTO section_info VALUES('$room_id','$section','$adviser','$level','$curriculum')");
    }
?> 

我的问题是,在我的section_info中,老师的名字没有属性,而是teacher_id。那么,我如何通过在下拉列表中选择教师的姓名来将teacher_id从"teacher_info"表插入到"section_info"表中。还是可能?谢谢!

$result= mysqli_query($con,"SELECT t_lname,t_fname,teacher_id FROM teacher_info");
echo "<select name='adviser' class='form-control' required>";
echo "<option value='0'>--Select Adviser--</option>";
while ($row=mysqli_fetch_array($result)) {
    echo "<option value='".$row['teacher_id']."'>".$row['t_lname'].", ".$row['t_fname']."</option>";
}

这样,您将$adviser变量中teacher_id值,随时可以使用。