0

I have three tables to connect on a single page. The first statement

if (($outcome['EmployeeNo'] = $data ['usn']

shows the numbers we need but the second one only outputs the number '1'.

($outcome['EmployeeID'] = $out['EmployeeID'])):

I'm using this to output them in href.

(viewlogs.php?id=<?= $data['id']; ?>&EmployeeNo=<?= $outcome['EmployeeNo'];?>&EmployeeID=<?= $out['EmployeeID'];)

but my condition is incorrect as it only shows outcome or out = 1.

Does anyone have a suggestion for this?

My complete codes:

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


  ?>

  <?php 


    try {

        include ("config.php");
        include ("sqlsrv.php");
     }catch(Exception $e){
        die("ERROR:".$e->getMessage());
     } 

    if(isset($_POST['usn']) && $_POST['usn']!=""){ 

         $re = $conn->prepare("SELECT EmployeeID, TimeLogID, RecordDate, RecordTime, Type, ActualTime FROM TA3.dbo.TimeLogs");
         $re->execute();

         $res = $conn->prepare("SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired, ResignationDate, EmployeeID FROM TA3.dbo.Employees");
         $res->execute();

         $req = $db->prepare("SELECT * FROM students WHERE usn LIKE :usn");
         $req->execute(array(
            'usn'=>'%'.$_POST['usn'].'%'));

         if ($req->rowCount()==0 && $_SESSION['type']=="Super_Admin") { ?>


            <span class="notfound">Student not found? <a class="addstud" href="create.php">add student?</a></span>

            <?php

         }
            elseif ($req->rowCount()==0 && $_SESSION['type']=="Admin") { ?>

                <span class="henhen">Student not found</span>

         <?php
            }
         else{
            while (($data=$req->fetch()) 
               && ($outcome=$res->fetch()) 
               && ($out=$re->fetch())){

                if (($outcome['EmployeeNo'] = $data ['usn']) && ($outcome['EmployeeID'] = $out['EmployeeID'])):

            ?>


            <div class="infohen">Info </div>
            <div class="uy">    
            <div class="name"><?php echo $data['fname']." ".$data['mname']." ".$data['lname']; ?></div> 
            <div class="email"><?php echo $data['email']; ?></div> 

            <div class="usn"><?php echo $data['usn']; ?></div>

            <div class="schedule"><?php echo $data['schedule']; ?></div>
            <div class="strand"><?php echo $data['strand']; ?></div></div>


            <?php if ($_SESSION['type']=="Super_Admin")
            { ?>
            <div class="gridme">
                <div class="action-list">
            <div class="action">Action</div>    
            <div class="edit"> <a href="edit.php?id=<?= $data['id']; ?>">Edit</a></div>
            <div class="logs"><a href="viewlogs.php?id=<?= $data['id']; ?>&EmployeeNo=<?= $outcome['EmployeeNo'];?>&EmployeeID=<?= $out['EmployeeID'];?>">Logs</a></div>
            <?php } else { ?>
            <div class="gridme">
                <div class="action-list">
            <div class="action">Action</div>    
            <div class="logs"><a href="viewlogs.php?id=<?= $data['id']; ?>&EmployeeNo=<?= $outcome['EmployeeNo'];?>&EmployeeID=<?= $out['EmployeeID'];?>">Logs</a></div>
            <?php } 
            endif; ?>
            </div>
        </div>




                </div>
                </div>


        <?php
        }
    }

 }else { ?>


        <span class="message">Enter USN!</span>

        <?php
    }


  ?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • If you write `if (A = B)` in your condition it will be true all the time since you just assign value, `if (A == B)` will compare the value as you want and `if (A === B)` will do the same + checking the value type – Mickaël Leger Mar 19 '18 at 08:54
  • I tried it, but it still outputs EmployeeID=1. –  Mar 19 '18 at 08:59
  • When you do your `while` loop, has your 3 request you use as condition (`($data=$req->fetch()) && ($outcome=$res->fetch()) && ($out=$re->fetch())`) the same length? Did you tried to check each value one by one too to see if you get what you want? `var_dump($outcome['EmployeeNo'])`, `var_dump($data ['usn'])`, etc. ? – Mickaël Leger Mar 19 '18 at 09:03
  • I think the problem is here, your `while` loop will run if and ONLY if you have `($data=$req->fetch()) && ($outcome=$res->fetch()) && ($out=$re->fetch())`, so if ONE of those request return only ONE result you will only go ONE time in your loop. – Mickaël Leger Mar 19 '18 at 09:06
  • I used var_dump (thank you! I'm a beginner and have never used this.) and EmployeeNo shows only '1', I'm trying to use it for dynamic page and I'm not quite sure why it only outputs 1. I have 4 EmployeeIDs as of now. –  Mar 19 '18 at 09:09
  • I would suggest that this module is mixing a lot of concerns that look _really wrong_ to me (disclaimer: I'm not a PHP dev). It directly knows about HTTP headers, HTTP requests/query params, session management, talking to a SQL database and rending HTML. Could someone who is a PHP dev offer some suggestions on how to clean it up? – poida Mar 19 '18 at 09:10
  • @Deus 1/ Btw you can replace `if(isset($_POST['usn']) && $_POST['usn']!=""){` by `if(!empty($_POST['usn'])` I think since empty check if it exists and if not empty / null. (check doc for more information). 2/ If you use `fetch` you will only get one result, try a `fetchAll` maybe before your condition and a `var_dump()` on the result to see what you get for each request 3/ Try to build what you want according to what your get for each result after checking what you get (step 2/) – Mickaël Leger Mar 19 '18 at 09:17

0 Answers0