gpt4 book ai didi

php - $_Session[variable] 放在代码中的什么位置

转载 作者:行者123 更新时间:2023-11-29 16:07:50 24 4
gpt4 key购买 nike

我试图在 $_Session[deptselect] 中保存一个变量并在不同的页面中调用它,但对在哪里声明它感到困惑。当我回显 $_Session[deptselect] 时,它给出空白。

我已经在多个地方尝试过,例如表单内部或表单下方,但似乎没有任何效果。我想将其从约会发送到约会2,但约会2似乎没有获取变量。我已经尝试使用 $_post[deptselect] 来获取第二页中的值,我得到了它,但一旦我在第二页中按下提交,它就会消失。

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");
?>

我想要在appointment2.php中使用$_session[deptselect],这样我就可以运行插入命令,我对js的了解为零,所以做依赖列表的事情是不可能的。 TIA

最佳答案

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

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

在第二个开头:

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

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

关于php - $_Session[variable] 放在代码中的什么位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55558602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com