用PHP填充两个下拉列表

时间:2014-12-31 06:43:08

标签: php mysql session

  • 我有3张桌子。
  • company表,departments表和imei numbers表。
  • 有2个下拉列表。
  • 首先从数据库中列出一家公司。
  • 选择公司后,它应显示所选公司的所有IMEI号码,并显示第二个下拉列表,其中包含所选公司数据库的部门列表。

代码:

$query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' ORDER BY `imei` ASC");   
  • 现在选择了一家公司。
  • 然后从下一个列表中选择一个部门。
  • 然后它应显示所选部门和所选公司的所有IMEI号码。

代码:

 $query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' AND `depid` = '".$_SESSION['depid']."' ORDER BY `imei` ASC");
  • 如果您想选择同一公司的其他部门,则应显示第二部门和所选公司的所有IMEI号码。
  • 到目前为止,一切正常。
  • 我为此使用了IF循环。
  • 检查depid $_SESSION['depid']的会话变量值。
  • 如果您想查看其他公司的imei号码,请选择其他公司。
  • 然后$_SESSION['companyid']将被更改。
  • $_SESSION['depid']将保留在先前所选部门的下限中。
  • 因此,由于$_SESSION['depid']不为空,else部分将再次执行。
  • 这意味着

代码:

$query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' AND `depid` = '".$_SESSION['depid']."' ORDER BY `imei` ASC");

将被执行。

  • 但是,在此查询中,$_SESSION['companyid']将是第二个选定公司的公司ID,而$_SESSION['depid']将是之前所选部门的部门ID。
  • 这表示$_SESSION['depid']不属于所选的$_SESSION['companyid']
  • 数据库中没有任何IMEI编号。
  • 因此,它不会给出第二个选定公司的IMEI号码列表,因为查询无法执行。
  • 我也试过了unset($_SESSION['depid']),但它没有用。
  • 如何查看第二选择公司的IMEI号码列表,当时已经选择了公司的部门?

  • 还有其他办法吗?超过IF循环

整个代码工作

<?php
    session_start();
    //$_SESSION['companyid'] = $_POST['companyid'];
    //$_SESSION['depid'] = $_POST['depid'];
(isset($_POST['companyid']) ? $_SESSION['companyid'] = $_POST['companyid'] : unset($_SESSION['companyid']));
(isset($_POST['depid']) ? $_SESSION['depid'] = $_POST['depid'] : unset($_SESSION['depid']));

    include "dbconnect.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

    <?php
        echo "<input type='hidden' id='comp' value='".$_SESSION['companyid']."'/>";
        echo "<input type='hidden' id='dept' value='".$_SESSION['depid']."'/>";
        echo "<input type='hidden' id='dept2' value='".$_SESSION['depid2']."'/>";

        echo "<form method='post'>";
        if ($_SESSION['depid'] == 0){
            $companyid = $_POST['companyid'];
            echo "<select name='companyid' id='companyid' onchange='form.submit()'>";
            echo "<option value=''>Select Company</option>";
            $query = mysql_query ("SELECT * FROM `company` ORDER BY `companyid` ASC") or die (mysql_error());
            while ($row = mysql_fetch_assoc($query)){
                echo "<option ".(($_SESSION['companyid'] == $row['companyid'])? 'selected==selected':'')." value='".$row['companyid']."'>".$row['company']."</option>";
            }
            echo "</select></br>";

            $depid = $_POST['depid'];
            $query = mysql_query("SELECT `depid`, `department` FROM `department` WHERE `companyid` = '".$_SESSION['companyid']."' ORDER BY `depid` ASC") or die (mysql_error());
            echo  "</br>";
            echo "<select name='depid' id='depid' onchange='form.submit()'>";
            echo "<option value=''>Select Department</option>";
            while ($row = mysql_fetch_assoc($query)){
                echo "<option ".(($_SESSION['depid'] == $row['depid']) ? 'selected==selected':'')." value='".$row['depid']."'>".$row['department']."</option>";
            }
            echo "</select></br>";
            //$query = "SELECT `depid`, `department` FROM `department` WHERE `companyid` = '".$_SESSION['companyid']."'";
            //echo $query;
            $query = mysql_query("SELECT `depid`, `department` FROM `department` WHERE `companyid` = '".$_SESSION['companyid']."'") or die (mysql_error());

            echo "<table>";
            echo     "<tr>";
            echo         "<th>IMEI NO.</th>";       
            echo     "</tr>";
            echo "</table>";
            $query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' ORDER BY `imei` ASC");
            echo "<table>";
            while ($row = mysql_fetch_assoc($query)){
                echo "<tr>";
                echo     "<td>".$row['imei']."</td>";
                echo "</tr>";
            }
            echo "</table>";
        }
        else {
            $companyid = $_POST['companyid'];
            echo "<label>test</label>";
            echo "<select name='companyid' id='companyid' onchange='form.submit()'>";
            echo "<option value=''>".$row['company']."</option>";
            $query = mysql_query ("SELECT * FROM `company` ORDER BY `companyid` ASC") or die (mysql_error());
            while ($row = mysql_fetch_assoc($query)){
                echo "<option ".(($_SESSION['companyid'] == $row['companyid'])? 'selected==selected':'')." value='".$row['companyid']."'>".$row['company']."</option>";
            }
            echo "</select></br>";

            $depid = $_POST['depid'];
            $query = mysql_query("SELECT `depid`, `department` FROM `department` WHERE `companyid` = '".$_SESSION['companyid']."' ORDER BY `depid` ASC") or die (mysql_error());
            echo "</br>";
            echo "<label>test</label>";
            echo "<select name='depid' id='depid' onchange='form.submit()'>";

            echo "<option value=''>Select Department</option>"; 

            while ($row = mysql_fetch_assoc($query)){
                echo "<option ".(($_SESSION['depid'] == $row['depid']) ? 'selected==selected':'')." value='".$row['depid']."'>".$row['department']."</option>";
            }
            echo "</select></br>";

            //$query = "SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' AND `depid` = '".$_SESSION['depid']."'";
            //echo $query;
            echo "<table>";
            echo     "<tr>";
            echo         "<th>IMEI NO.</th>";       
            echo     "</tr>";
            echo "</table>";

            $query = mysql_query ("SELECT `imei` FROM `devicemanager` WHERE `companyid` = '".$_SESSION['companyid']."' AND `depid` = '".$_SESSION['depid']."' ORDER BY `imei` ASC");
            echo "<table>"; 
            while ($row = mysql_fetch_assoc($query)){
            echo     "<tr>";
            echo         "<td>".$row['imei']."</td>";
            echo     "</tr>";
            }
            echo "</table>";
        }
        echo "</form>";
    ?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

首先,您确实应该清理查询并使用mysqli

更改代码:

$_SESSION['companyid'] = $_POST['companyid'];
$_SESSION['depid'] = $_POST['depid'];

为:

(isset($_POST['companyid']) ? $_SESSION['companyid'] = $_POST['companyid'] : unset($_SESSION['companyid']));
(isset($_POST['depid']) ? $_SESSION['depid'] = $_POST['depid'] : unset($_SESSION['depid']));