1

我有两张桌子,employee作为父母和license孩子。他们都有一Lic_ID列供参考,这一列是PKinlicenseFKin employee。该license表还有一个Lic_Type包含许可证名称的列。

我正在尝试创建一个带有列表框的employee表格,以便可以更新表格。列表框value需要填充license.Lic_ID并且license.Lic_Type将显示在 中option。这是我所拥有的:

(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>";
?>

这样效果很好,它显示了许可证类型并将值设置为许可证 ID。<option selected="selected">如果为员工设置了许可证 ID,我想要做的是。这段代码不起作用,但我认为它说明了我正在尝试做的事情:

<?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>";
?>

有没有办法完成我想做的事情?

4

2 回答 2

1

我认为我到底想要完成什么可能有些混乱,我很抱歉不是很清楚。无论如何,我今天偶然发现了答案,所以我想我应该发布它。

$sql1 = ("SELECT Emp_Name, Lic_MAT_ID FROM employee");

if(!$result_employee_query = $mysqli->query($sql1))
    {
    die ("There was an error getting the records from the employee table");
    }

while($employee = $result_employee_query->fetch_assoc())
    {
    echo "Employee Name: " . $employee['Emp_Name'] . "<br>";

    echo "License: ";
    echo "<select>";

    $sql2 = ("SELECT Lic_MAT_ID, Lic_MAT_Type FROM license_mat");

    if(!$result_license_query = $mysqli->query($sql2))
        {
        die ("There was an error getting the records from the license table");
        }

    while($license = $result_license_query->fetch_assoc())
        {
        echo "<option value=\"" . $license ['Lic_MAT_ID'] . "\"";
            if($license['Lic_MAT_ID'] == $employee['Lic_MAT_ID'])
                {
                echo " selected=\"selected\"";
                }
        echo ">" . $license ['Lic_MAT_Type'] . "</option>";
        }

    echo "</select><br>";
    }
于 2012-12-04T09:29:54.253 回答
0

据我了解您的问题:您想查看许可证是否已添加到任何用户或未分配。如果任何用户都设置了许可证,则它被“选中”,否则不会。

首先,您必须将关键字分配"multiple"给您的select对象以使其成为列表框:

echo "<select name=\"Lic\" multiple=\"multiple\">";

第二:我会写这样的查询:

$sql = $mysqli->query("SELECT l.Lic_ID, l.Lic_Type, e.cnt FROM licence l left outer join (select Lic_id, count(*) cnt from employee group by Lic_id) e on l.Lic_ID=e.Lic_id");

它选择Lic_ID,Lic_Typecount有多少员工设置了相应的 Lic_ID ( left outer join)

并在代码中检查,如果count高于 0

if($row['cnt'] > 0){ 
  echo "selected=\"selected\";
}
于 2012-11-19T08:43:26.647 回答