我写了两个php文件,当他们输入正确的用户时会在数据库中显示用户的信息,如果他们重新打开浏览器就会再次显示它。但是,当我重新打开浏览器时,我收到此错误消息:
Notice: Undefined index: full_name in C:\xampp\htdocs\test\session-database-2.php on line 3
wrong user please type again
Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\test\session-database-1.php on line 1
1st是session-database-1.php
<?php session_start() ?>
<?php
echo "login Form";
echo "<form action='session-database-2.php' method='POST'>
name <input type='text' name='full_name' />
<input type='submit' value='submit'/>
</form>";
?>
第二是
<?php session_start() ?>
<?php
$_SESSION['full_name']=$_POST['full_name'];
$host='localhost';
$username='root';
$password='root';
$dbname='pet';
$connect=mysqli_connect($host,$username,$password,$dbname) or die("can't connect to server");
$query="SELECT * FROM register WHERE full_name='{$_SESSION['full_name']}'";
$result=mysqli_query($connect,$query) or die("can't execute query");
if(mysqli_affected_rows($connect))
{
while($row=mysqli_fetch_assoc($result))
{
extract($row);
echo $full_name."<br/>";
echo $email."<br/>";
echo $phone."<br/>";
}
}
else
{
echo "wrong user please type again";
include "session-database-1.php";
exit();
}
?>
答案 0 :(得分:1)
我认为还有一些错误。
session-database-1.php
中的1.行,因为那里没有使用$ _SESSION Undefined index: full_name
是因为您直接调用此网址时没有$ _POST session_start
被调用两次。您可以在一个文件中解决它:
if(isset($_POST['full_name']))
// Then show your login form
else
// make your database select and show the results
答案 1 :(得分:0)
如果您希望数据保持不变,即使浏览器已关闭,您也无法使用会话。正如Pat在评论中提到的,一旦浏览器关闭,会话就会被破坏。这个SO线程可能会帮助您理解两者之间的区别