【问题标题】:mysqli join two tables to match user name with the corresponding user idmysqli连接两个表来匹配用户名和对应的用户id
【发布时间】:2015-03-16 11:10:59
【问题描述】:

我很难弄清楚这一点..

我有两个表“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 中没有教师姓名的属性,而是有教师 ID。那么,我如何通过在我的下拉列表中选择老师的姓名,将“teacher_info”表中的teacher_id 插入“section_info”表。或者有可能吗? 谢谢!

【问题讨论】:

    标签: php mysqli inner-join


    【解决方案1】:
    $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 值,可以使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 2018-05-08
      相关资源
      最近更新 更多