将foreach循环限制为仅限一个用户?

时间:2018-03-18 19:52:44

标签: php pdo

我有一个系统,我想将foreach限制为指定用户:

我的输出显示每个注册日期,我可以将其限制为只有一个,但如果我这样做,那么所显示的单个内容也将显示给每个用户。

enter image description here

这应该只显示一个已登记和未登记的,但我不知道是否应该使用if语句或使用行查询。我尝试了它,但它只输出错误"未捕获错误:调用数组中的成员函数result_array()。 。 "

(下面是我尝试使用row的代码,但输出上述错误。)

    <?php 
    $result_array = $res->result_array();
    $results = array();
    foreach ($res->result_array() as $row)
    {
        $results[] = $row;
    } 
    ?>

这是我的代码最新代码,名为viewlogs.php

<?php
    session_start();
        if (!isset($_SESSION['username']))
        {
            header('location: login.php');
        die();
        }


    ?>
    <?php
        require"config.php";
        require"sqlsrv.php";

         $id=$_GET['id'];

         $sql= 'SELECT * FROM students WHERE id= :id';

         $stmt = $db-> prepare($sql);

         $stmt->execute([':id' => $id ]);

         $data= $stmt->fetch(PDO::FETCH_OBJ);

        $query= 'SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired, ResignationDate FROM TA3.dbo.Employees';
            $statement = $conn->query($query);
            $statement->execute();

        $res=$statement->fetchAll(PDO::FETCH_OBJ);

        $q= 'SELECT TimeLogID, EmployeeID, RecordDate, RecordTime, Type, ActualTime FROM TA3.dbo.TimeLogs';
        $st = $conn->query($q);
        $st->execute();

        $re=$st->fetchAll(PDO::FETCH_OBJ);

         if (isset ($_POST['fname']) && (isset($_POST['lname']) && (isset($_POST['email']) && (isset($_POST['usn']) && (isset($_POST['schedule']) && (isset($_POST['year']) && (isset($_POST['strand']))))))))
         {

          $fname = $_POST['fname'];
          $mname = $_POST['mname'];
          $lname = $_POST['lname'];
          $email = $_POST['email'];
          $usn = $_POST['usn'];
          $schedule = $_POST['schedule'];
          $year = $_POST['year'];
          $strand = $_POST['strand'];
        }

        if (isset ($_POST['DateHired']) && (isset($_POST['ResignationDate']) && (isset($_POST['EmployeeNo']))))
        {
            $DateHired = $_POST['DateHired'];
            $ResignationDate = $_POST['ResignationDate'];
            $EmployeeNo = $_POST['EmployeeNo'];
        }

        if (isset ($_POST['TimeLogID']) && (isset($_POST['EmployeeID']) && (isset($_POST['RecordDate']) && (isset($_POST['RecordTime']) && (isset($_POST['Type']) && (isset($_POST['ActualTime'])))))))
        {

          $TimeLogID = $_POST['TimeLogID'];
          $EmployeeID = $_POST['EmployeeID'];
          $RecordDate = $_POST['RecordDate'];
          $RecordTime = $_POST['RecordTime'];
          $Type = $_POST['Type'];
          $ActualTime = $_POST['ActualTime'];
        }
        ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title>View Logs</title>
        <link rel="icon" type="image/png" sizes="16x16" href="image/favicon-16x16.png">
        <link rel="icon" type="image/png" sizes="32x32" href="image/favicon-32x32.png">
        <meta name= "viewport" content="width= device-width, initial-scale=1">
        <script defer src="js/fontawesome-all.js"></script>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <link rel="stylesheet" type="text/css" href="css/viewlogs.css">
    </head>

    <body>


        <div class="back"><a class="henny" href="students.php"><i class="fas fa-backward"></i> Back </a></div>
    <div class="title"> <h1>Student Logs<span class="blink">_</span></h1></div>

        <div class="grid">


            <div class="student-info">

            <label>First name</label>
            <input value="<?= $data->fname; ?>" type="text" name="fname" readonly/>
            <label>Middle name</label>
            <input value="<?= $data->mname; ?>" type="text" name="mname" readonly/>
            <label>Last name</label>
            <input value="<?= $data->lname; ?>" type="text" name="lname" readonly/>
            <label>USN</label>
            <input value="<?= $data->usn;  ?>" type="text" name="usn" readonly/>
            <label>Strand</label>
            <input value="<?= $data->strand; ?>"  type="text" name="strand" readonly/>
            <label>Schedule</label>
            <input value="<?= $data->schedule;  ?>" type="text" name="schedule" readonly/>



            </div>

            <div class="device-info">
            <?php 

            $i = 0;
            foreach ($res as $outcome): ?>
            <label>Enrolled since</label>
            <input value="<?= date('Y-m-d', strtotime($outcome->DateHired)); ?>" type="text" name="enrolled" readonly/>

            <label>Not Enrolled since</label>
            <input value="<?= $outcome->ResignationDate; ?>" type="text" name="notenrolled" readonly/>
            <?php endforeach; ?>

            </div>

                <div class="logs">

                    <table style="width:100%" class="logs-table">
                <tr>
                    <th style="width:25%">Date</th>
                    <th style="width:25%">Time</th>
                    <th style="width:12%">In</th>
                    <th style="width:12%">Break in </th>
                    <th style="width:12%">Break out</th>
                    <th style="width:12%">Out</th>

                </tr>
    </table>

            </div>


        </div>
        <?php  



            $r = $db->prepare("SELECT * FROM students WHERE id LIKE :id");
            $r->execute(array( ':id'=>'%'.$id. '%'));

            if ($data=$r->fetch())
            {
        ?>



        <a class="print" target="_blank" href="print.php?id=<?= $data['id']; ?>"> Print <i class="fas fa-print"></i></a>

           <?php
                    } 
            ?>


    </body>
    </html>

和我的sqlsrv.php

<?php 
    /*
    Connection for sqlsrv
    */
    try {
        $conn = new PDO( 'sqlsrv:server=(localdb)\\v11.0;'
            . 'AttachDBFileName=C:\\PROGRAMDATA\\TOUCHLINK TIME RECORDER 3\\TA3.mdf;Database=TA3');

        $query = 'SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired FROM TA3.dbo.Employees ORDER BY EmployeeNo ASC';
        $stmt = $conn->query($query);
        $stmt->execute();
    } 
    catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }

?>

1 个答案:

答案 0 :(得分:0)

IIUC - 通过将已发布的HTML表单返回的值作为参数传递,只需通过特定的 EmployeeNo (或Web用户的任何标识符)过滤您的查询。这要求您在分配$_POST值后更改运行查询的代码顺序。

...

if (isset ($_POST['DateHired']) && (isset($_POST['ResignationDate']) && (isset($_POST['EmployeeNo']))))
{
   $DateHired = $_POST['DateHired'];
   $ResignationDate = $_POST['ResignationDate'];
   $EmployeeNo = $_POST['EmployeeNo'];
}

...

$query= 'SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired, ResignationDate  
         FROM TA3.dbo.Employees
         WHERE EmployeeNo = ?';

$statement = $conn->prepare($query);
$statement->bind_param(1, $EmployeeNo);

$statement->execute();

$res = $statement->fetchAll(PDO::FETCH_OBJ);