【发布时间】:2016-01-05 16:58:34
【问题描述】:
我开发了一个简单的搜索页面,但似乎无法正常工作。我面临两个问题,我认为这是相关的。当我进入搜索表单页面时,错误:
注意:未定义索引:用户在第 36 行登录 header.php 注意:未定义索引:在第 59 行管理登录 header.php`
当使用有效测试数据使用搜索表单时,会显示这些错误:
注意:会话已经开始 - 忽略第 3 行 header.php 中的 session_start() 注意:未定义索引:用户登录第 36 行 header.php 注意:未定义索引:管理登录第 59 行 header.php mysqli_stmt::bind_result():绑定变量的数量与第 12 行 search.php 中准备好的语句中的字段数量不匹配
我不会发布 header.php,因为我认为这是因为 bind_result,但是我不确定我是怎么做错的。
我已尝试从 header.php 中删除 session_start(),因此它不会自动包含在内,但这只会引发此错误:
注意:未定义变量:第 36 行 header.php 中的 _SESSION 注意:未定义变量:第 59 行 header.php 中的_SESSION
Searchform.php
<?php
ob_start();
error_reporting(E_ALL);
?>
<br><br><br>
<?php
ini_set('display_errors', -1);
include 'header.php';
include 'connection.php';
?>
<br /><br />
<html>
<header>
<link rel="stylesheet" type="text/css" href="web.css" />
<link rel="stylesheet" type="text/css" href="/web.css" />
</header>
<body>
<center>
<h2>Search for people</h2>
<br />
Please enter the name you wish to search for<br /><br />
<form method="post" action="search.php" id="searchform">
<input type="text" name="name">
<input type="submit" name="searchsubmit" value="Submit">
</form>
<br /><br/>
Or search people by category
</center>
</body>
</html>
Search.php
<?php
if(isset($_POST['searchsubmit']))
{
include 'searchform.php';
include 'header.php';
$name = $_POST['name'];
if ($stmt = $connection->prepare ("SELECT * FROM users WHERE Username = ?"))
{
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($personresult);
$stmt->fetch();
print_r($personresult);
}
else
{
echo "<p>Please enter a search query</p>";
}
}
else
{
echo "NOT SET!";
}
?>
【问题讨论】:
-
你必须在每一页上开始你的会话。
-
在 session_start 之前不能有任何输出,这意味着三个中断必须在 包含之后。
-
@ChrisG 这是一个误导性的评论,因为每次客户端从服务器请求时,是的,每次都需要启动一个会话。不过,您不需要在您使用的每个包含的脚本上启动它。