php - 尝试从下拉列表中获取 EmployeeID 和 EName 以在查询中使用以显示表

标签 php mysql

我正在开发一个培训矩阵/跟踪网站。我从另一个表的数据库中填充了下拉列表。我能够显示来自另外两个表的表数据,这些表引用用户 ID,在顶部显示用户的名称,然后显示一个在下面完成所有训练的表。

我已经使下拉菜单正常工作,但我正在努力让数据库使用下拉框中设置的 id 将数据提取到表中。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
    header('Location: index.html');
    exit();
}
?>

<?php
include ("templates/header.php");
include ("connect-db.php");
?>
<?php
$EmployeeID = $_POST['Get'];
    $sqlEmployee = "SELECT EmployeeID, EName FROM employee ORDER BY EName";
    $stmtEmployee = $dbCon->prepare($sqlEmployee);
    $arrEmployee = array();
    $stmtEmployee->bindValue(':EmployeeID', $EmployeeID); 
        if ($stmtEmployee->execute()) {
                $arrEmployee = $stmtEmployee->fetchAll(PDO::FETCH_ASSOC);
}

if (!isset($_POST['EmployeeID'])) {

    $sql = "SELECT * FROM employee";
    $stmtTable = $dbCon->prepare($sql);
    $stmtTable->execute();
}
?>
<center>
<form action="search.php" method="post">

<label for="EmployeeID">Employee</label>
    <select name="EmployeeID" id="EmployeeID">
    <?php
      for($i=0;$i<count($arrEmployee);$i++) {
         $row = $arrEmployee[$i];
      ?>
      <option value="<?= $row['EmployeeID'] ?>" <?php echo "Selected='selected'"?> ><?= $row['EName'] ?></option>
      <?php
      }
    ?>
    </select>
    <input type="submit" name="Get" value="Add New Record"></button>
</center>

<?php
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Home</th>
<th>Shift</th>
<th>Start Date</th>


</tr>";

while ($rowTable = $stmtTable->fetch($sql)) 
{
echo "<tr>";
echo "<td>" . $rowTable['EName'] . "</td>";
echo "<td>" . $rowTable['HomeBase'] . "</td>";
echo "<td>" . $rowTable['Shift'] . "</td>";
echo "<td>" . $rowTable['StartDate'] . "</td>";
echo "</tr>";
}
echo "</table>";
$stmtTable=Null

?>

<?php
include ("templates/footer.php");
?>

我收到一个错误,它没有获取变量。我知道如何加入数据库,但很难获得简单的名称转换开始日期数据。

更新后的新代码

<?php

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    // We need to use sessions, so you should always start sessions using the below code.
    session_start();

    // If the user is not logged in redirect to the login page...
    if (!isset($_SESSION['loggedin'])) {
        header('Location: index.html');
        exit();
    }
    //includes for page
    include ("templates/header.php");
    include ("connect-db.php");
    // This one below is for the dropdown box to populate the user name

    $EmployeeID = $_POST['EmployeeID']; //changed this to EMployeeID from Get
    $sqlEmployee = "SELECT EmployeeID, EName FROM employee ORDER BY EName";
    $stmtEmployee = $dbCon->prepare($sqlEmployee);
    $stmtEmployee->bindValue(':EmployeeID', $EmployeeID);

    $arrEmployee = array();

    if ($stmtEmployee->execute()) {
        $arrEmployee = $stmtEmployee->fetchAll(PDO::FETCH_ASSOC);
    }

?>
<center>
    <form action="search.php" method="post">
        <label for="EmployeeID">Employee</label>
        <select name="EmployeeID" id="EmployeeID">
<?php
        //this is a dropdown box for the top of the page to be able to select user to display data on

            for($i=0;$i<count($arrEmployee);$i++) {
                $row = $arrEmployee[$i];
                echo '<option value="' . $row['EmployeeID'] . '">' . $row['EName'] . '</option>';
            }

        ?>
        </select>
        <button type="submit" name="Get">Get</button>
    </form>
</center>

<table border='1'>
    <thead>
        <tr>
            <th>Name</th>
            <th>Home</th>
            <th>Shift</th>
            <th>Start Date</th>
        </tr>
    </thead>
    <tbody>
   <?php
        //this is for the data in the table to be populated using the dropdown as the source for the id to display the user data ect.
    if (!isset($_POST['EmployeeID'])) {
        $sql = "SELECT * FROM employee Where EmployeeID = $EmployeeID";
        $stmtTable = $dbCon->prepare($sql);
        $stmtTable->execute();

        while ($rowTable = $stmtTable->fetch()) {
            echo "<tr>";
            echo "<td>" . $rowTable['EName'] . "</td>";
            echo "<td>" . $rowTable['HomeBase'] . "</td>";
            echo "<td>" . $rowTable['Shift'] . "</td>";
            echo "<td>" . $rowTable['StartDate'] . "</td>";
            echo "</tr>";
        }
    } else {
        echo '<tr><td colspan="4">No records found</td>';
    }

    ?>
    </tbody>
</table>

<?php include ("templates/footer.php"); ?>

更新2 通过将 if (!isset($_POST['EmployeeID'])) 更改为 if (isset($_POST['EmployeeID'])) 这使得页面可以工作,但我仍然收到错误“注意:未定义索引:EmployeeID in C:\UniServerZ\www\search.php 位于第 19 行”,首次加载页面时,但单击名称后,该问题消失。

最佳答案

我按照 Adam Miller 的通知更改了代码(谢谢 Adam),然后我更改了:

!isset 到第二个查询上的 isset

$EmployeeID = $_POST['EmployeeID'] 
to
$EmployeeID = (isset($_POST['EmployeeID']) ? $_POST['EmployeeID'] : ''); 

这样就可以在检查是否有输入时消除错误。

再次感谢亚当,你太棒了。

关于php - 尝试从下拉列表中获取 EmployeeID 和 EName 以在查询中使用以显示表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57185621/

相关文章:

javascript - 动态显示按钮单击上的文本框

javascript - 根据第一个下拉列表中的选择动态填充 3 个下拉列表

php - 如果发生重定向,如何在 file_get_contents 之后获取真实的 URL?

php - Json在mysql中存储设置?

mysql - 关闭科学记数法 MySQL

mysql - 无法从subsonic 2.2插入到mysql(Joomla CMS数据库)

mysql - 使用 bash 脚本删除具有文件名模式的文件

php - anchor 标记用于在循环内的元素中滚动

php - 插入发帖的用户

php - Mysql、phpmyadmin、创建数据库