我正在使用一个培训矩阵/跟踪网站。我从另一个表的数据库中填充了下拉菜单。我能够显示来自另外两个表的表数据,这些表数据引用用户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'])),可以使页面正常工作,但我仍然收到错误消息“ Notice:Undefined index:EmployeeID in C:\ UniServerZ \ www \ search.php,位于第19行上”,这是第一次加载页面时的结果,但是在单击名称后,该信息消失了。

最佳答案

我为您提供的第一条建议是保持编码方法的一致性,并使代码井井有条。例如,您定义了$sql,但是不要像其他20行那样使用它。那是非常混乱的。每个人都必须从某个地方开始,所以请牢记我的批评。

话虽这么说,让我们重新组织一下代码。据我所知,您正在尝试显示一个表,其中包含员工的姓名,位置(总部),轮班和开始日期。只要数据库表包含该信息,就应该很容易完成。

我注意到的一件事是if (!isset($_POST['EmployeeID'])) {。我敢打赌这是你的问题。如果$_POST['EmployeeID']为空/未设置,则$sql为空。所以让我们做这样的事情:

<?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();
    }

    include ("templates/header.php");
    include ("connect-db.php");

    $EmployeeID = $_POST['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

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

        ?>
        </select>
        <button type="submit" name="Get">Ad New Record</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

    if (!isset($_POST['EmployeeID'])) {
        $sql = "SELECT * FROM employee";
        $stmtTable = $dbCon->prepare($sql);
        $stmtTable->execute();

        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>";
        }
    } else {
        echo '<tr><td colspan="4">No records found</td>';
    }

    ?>
    </tbody>
</table>

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

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

10-15 07:32