我有我的Web应用程序,并且它具有(注册表,添加信息,查看信息和搜索以及所有这些选择,都在导航栏菜单的同一页面中) 我为每个用户启动会话(登录时),一切正常,直到我搜索到某个东西,然后得到搜索结果,然后转到添加或查看我发现已注销的信息,这意味着会话破坏了为什么发生了什么?我希望用户即使在完成搜索后也可以登录,请帮助我并向我道歉,如果我输入错了。 这是我的搜索部分代码:
<form class="navbar-form navbar-left" action="" method="post">
<div class="input-group">
<input size=55 type="text" name="search" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button>
</span>
</div>
</form>
</nav>
</div>
<?php
if(!isset($_SESSION)){session_start();}
$conn = mysql_connect("localhost", "root", '');
mysql_select_db("db103",$conn);
$user=$_SESSION["myname"];
$fname = '';
if (isset($_POST['search']))
{
$fname = $_POST['search'];
}
else{}
if(isset($_SESSION["myname"])){
$sql = "SELECT * FROM telephone_guide where owner='$user' and (firstName like '%$fname%' or secondName like '%$fname%' or phonenumber like '%$fname%' or celnumber like '%$fname%')";
$result = mysql_query($sql, $conn) ;
// number of rows fetched
$num = mysql_num_rows($result);
echo("<div class='styl'>");
echo("<table width=300 border=3 >");
echo("<th>ID<th>First Name<th>Second Name<th>tele-Phone Number<th>cel_phone number<th>Date<th>Address");
while ($arr = mysql_fetch_array($result)) {
echo("<tr><td><a href='update1.php?id1=$arr[0]'>$arr[0]</td><td>$arr[2]</td><td>$arr[3]</td><td>$arr[4]</td><td>$arr[5]</td><td>$arr[6]</td><td>$arr[7]</td></tr>");
}
echo("</table>");
?>
<br>
<br>
<?php
echo("</div>");}
?>
答案 0 :(得分:-1)
您需要先初始化会话 您还可以在脚本的最后看到 ob_start()和 ob_flush_end()。这是为了抑制由于php中的html插入而已发送的数据包引起的错误
所以试试这个
<?php
ob_start();
session_start();
?>
<form class="navbar-form navbar-left" action="" method="post">
<div class="input-group">
<input size=55 type="text" name="search" class="form-control" placeholder="Search">
<span class="input-group-btn">
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button>
</span>
</div>
</form>
</nav>
</div>
<?php
if(isset($_SESSION)){}
$conn = mysql_connect("localhost", "root", '');
mysql_select_db("db103",$conn);
$user=$_SESSION["myname"];
$fname = '';
if (isset($_POST['search']))
{
$fname = $_POST['search'];
}
else{}
if(isset($_SESSION["myname"])){
$sql = "SELECT * FROM telephone_guide where owner='$user' and (firstName like '%$fname%' or secondName like '%$fname%' or phonenumber like '%$fname%' or celnumber like '%$fname%')";
$result = mysql_query($sql, $conn) ;
// number of rows fetched
$num = mysql_num_rows($result);
echo("<div class='styl'>");
echo("<table width=300 border=3 >");
echo("<th>ID<th>First Name<th>Second Name<th>tele-Phone Number<th>cel_phone number<th>Date<th>Address");
while ($arr = mysql_fetch_array($result)) {
echo("<tr><td><a href='update1.php?id1=$arr[0]'>$arr[0]</td><td>$arr[2]</td><td>$arr[3]</td><td>$arr[4]</td><td>$arr[5]</td><td>$arr[6]</td><td>$arr[7]</td></tr>");
}
echo("</table>");
?>
<br>
<br>
<?php
echo("</div>");}
?>
<?php ob_end_flush(); ?>