dongzhong1891 2012-11-19 04:32
浏览 144
已采纳

如何在加入mySQL表时从主键和外键中获取值?

I have two tables, employee as the parent and license as the child. They both have a Lic_ID column for reference, this column is the PK in license and the FK in employee. The license table also has a column Lic_Type which holds the name of the license.

I am trying to create a table with list boxes so the employee table can be updated. The list box value needs to be populated with the license.Lic_ID and the license.Lic_Type is to be displayed in the option. Here is what I have:

(Employee name, Id, etc. called out up here)

<?php
echo "<select name=\"Lic\">";
echo "<option value=\"\">Select...</option>";

$sql = $mysqli->query("SELECT Lic_ID, Lic_Type FROM license");

while($row = $result->fetch_assoc())
     {
     echo "<option value=\"" . $row['Lic_ID'] . "\">" . $row['Lic_Type'] . "</option>";
     }

echo "</select>";
?>

So that works good, it shows the license type and has the value set to the license id. What I want to do is have <option selected="selected"> if the license id is set for an employee. This code doesn't work, but I think it illustrates what I'm trying to do:

<?php
echo "<select name=\"Lic\">";
echo "<option value=\"\">Select...</option>";

$sql = $mysqli->query("SELECT license.Lic_ID, license.Lic_Type, employee.Lic_ID FROM employee INNER JOIN license ON employee.Lic_ID = license.Lic_ID");

while($row = $result->fetch_assoc())
     {
     echo "<option value=\"" . $row['license.Lic_ID'] . "\"";
         if($row['employee.Lic_ID'] = $row['license.Lic_ID']){echo "selected=\"selected\";}
     echo ">" . $row['license.Lic_Type'] . "</option>";
     }

echo "</select>";
?>

Is there a way to accomplish what I'm trying to do?

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 实值特征权重向量和特征选择,多分类
      • ¥15 INVEST年产水量模块结果出处理
      • ¥15 Neo4j 不显示关系
      • ¥15 这段代码有什么bug
      • ¥15 pip默认安装位置修改不成功
      • ¥15 C++ inline、staitc、const三合一谁能讲通透
      • ¥15 关于pyqt5中QThread的问题
      • ¥20 STM32基于HAL库使用串口发送16进制数组
      • ¥15 OpenCV报错 You can enable it via 'OPENCV_IO_ENABLE_OPENEXR'
      • ¥15 AT89C51的程序问题