PHP会话变量丢失在同一页面上

I have Teams that I am assigning Employees to. The manager logs into his/her account which assigns the Team Number. They then click a link (which passes the Team ID as a variable) to go to the Assignment Page where they do an Employee Lookup; find the Employee and click on a link to assign this employee to their team.

When they first enter this Assignment Page, the Team Number exists. After they do the Search, however, the Team Number is blanked out. This is all on the same page. I don't know why the Search function wipes out the Session Variable value. Thank you.

<?php 
session_start();
?>

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <?php 
    $_SESSION["teamid"] = $_REQUEST["tid"];
    ?>

    <?php 
    $con = new mysqli($localhost, $username, $password, $dbname); 
    if( $con->connect_error){
       die('Error: ' . $con->connect_error);
    } 
    if( isset($_GET['search']) ){  
      $team = $_SESSION["teamid"];
      $memberid = mysqli_real_escape_string($con, htmlspecialchars($_GET['search'])); 
      $sql = "SELECT * FROM employees WHERE empid ='$memberid'"; 
    } 
    $result = $con->query($sql);
    ?>

    <label>Enter Employee You Wish To Add To Your Team (<?php echo $_SESSION["teamid"]; ?>)</label>
    <form action="" method="GET">
        <input type="text" placeholder="Enter Employee ID here" name="search">&nbsp;
        <input type="submit" value="Search" name="btn" class="btn btn-sm btn-primary">
    </form>
    <br />
    <table class="table table-striped table-responsive">
        <tr>
            <th>Employee ID</th>
            <th>Name</th>
            <th>Action</th>
        </tr>
        <?php 
        while($row = $result->fetch_assoc()){
            ?>
            <tr>
                <td><?php echo $row['empid']; ?></td>
                <td><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></td>
                <td><a href="assignemployee.php?mid=<?php echo $row['empid']; ?>&tid=<?php echo $team; ?>">Assign Employee</a></td>
            </tr>
            <?php
        }

        ?>
    </table>
</div>

</body>
</html>

It's because at the top of your code you have this part:

<?php 
$_SESSION["teamid"] = $_REQUEST["tid"];
?>

So everytime the page reloads it sets the teamid to whatever is in the request. The second time you visit the page it's most likely empty which causes the session to be empty too.

An easy way to fix that is to check if $_REQUEST["tid"] is set:

<?php
if (isset($_REQUEST["tid"])) {
    $_SESSION["teamid"] = $_REQUEST["tid"];
}
?>