在代码中将$ _Session [variable]放在哪里

时间:2019-04-07 11:38:28

标签: php mysql

我试图在$ _Session [deptselect]中保存一个变量,并在另一个页面中调用它,但对声明它的位置感到困惑。当我回显$ _Session [deptselect]时,它为空白。

我已经在表格内或表格下方的多个位置尝试过,但似乎没有任何效果。我想将其从约会发送到约会2,但约会2似乎没有获取变量。 我已经尝试过使用$ _post [deptselect]在第二页中获取该值,然后在那儿获取它,但是一旦我在第二页中按Submit,它就会消失。

appointment.php

<?php
include("dbconnection.php");
session_start();
if(!isset($_SESSION['username'])) {
  die("Please login");
}
$_SESSION['deptselect']=$_POST['deptselect'];
?>

<form class="" action="appointment2.php" method="post">  
  <fieldset>
    <legend>Appointment</legend>    
    <label>Select Department</label>
    <select class="" name="deptselect" required>
      <option ></option>
      <?php      
      $deptsql=mysqli_query($con,"select deptname,deptid from department where status='1'");
      while ($row = mysqli_fetch_assoc($deptsql))
      {
        echo "<option id='deptselect' value='" . $row['deptid'] ."'>" . $row['deptname'] ."</option>";
      }      
      ?>
    </select>
  </fieldset>
  <input type="submit" name="submit" value="Submit">
</form>

<?php
include("footer.php");
?>
appointment2.php

<?php
session_start();
if(!isset($_SESSION['username']))
{
  include('footer.php');
  die("Please login");
}
if(!empty($_POST)){
  include("dbconnection.php");
  $dept=$_SESSION['deptselect'];
  echo $dept;
  $daate=$_POST['date'];
  $doc = $_POST['doc'];
  $tiime=$_POST['time'];
  $user=$_SESSION['username'];
  $pd="SELECT * from patient where name='$user'";
  $pid=mysqli_query($con,$pd);
  while($row = mysqli_fetch_assoc($pid)){
    $piid=$row['pid'];
  }
  $query="insert into appointment (deptid, docempid,pid,apptime,appdate) VALUES ('$dept','$doc','$piid','$tiime','$daate') " ;
  //$res=mysqli_query($con,$query);
//  echo "$query";
  //echo "$dept";
}
?>
<body>
  <form class="" action="appointment2.php" method="post">
    <label>Select doctor</label>
    <select class="" name="doc" >
      <option ></option>
      <?php
      $dee=$_POST['deptselect'];
      $_SESSION['id']=$dee;
      $sql="SELECT * FROM doctor where  deptid='$dee'";
      $docsql= mysqli_query($con,$sql);
      while ($row = mysqli_fetch_assoc($docsql))
      {
        echo "<option value='" . $row['docempid'] ."'>" . $row['name'] ."</option>";
      }
      ?>
    </select>
    <br><br><br>
    <label>Enter Time</label>
    <input type="time" name="time" placeholder="enter time" >

    <br><br><br>
    <label>Enter date</label>
    <input type="date" name="date" placeholder="enter date" >
    <br> <br>

    <input type="submit" name="submit" value="Submit">

  </form>

</body>

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

我希望在pointing2.php中使用$ _session [deptselect],以便我可以运行命令insert,因为我对js的了解为零,所以无法执行依赖列表。 TIA

1 个答案:

答案 0 :(得分:0)

在第一个脚本的开头,您有:

if(!isset($_SESSION['username'])) {
    die("Please login");
}

在第二个开始时:

if(!isset($_SESSION['username']))
{
    include('footer.php');
    die("Please login");
}

如果未设置$_SESSION['username'],两个都立即停止。 如果您的 body 代码从未执行过,那么应该如何设置$_SESSION['username']