我正在使用下面的代码显示搜索,它正在运行。但是当我尝试显示另一个带有其他结果的页面时,它会说“索引未定义”。我在这个文件中再次使用但它没有解决任何问题。有谁能告诉我为什么会这样?
错误是:
注意:未定义的索引:第32行的J:\ xampp \ htdocs \ pesquisa3.php中的pesquisa
代码是:
<?php
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: login.php");
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html"; charset="utf-8"/>
<title>Registo</title>
</head>
<body background="/images/farm.jpg">
<center>
<h1>Pesquisa em Mugidor</h1>
<?php
$dbh = mysql_connect("127.0.0.1","root","")
or die("Erro ao ligar-me a base de dados -> ".mysql_error());
$db = mysql_select_db("users", $dbh)
or die("Erro ao escolher a base de dados -> ".mysql_error());
mysql_set_charset('utf8', $dbh);
// verificação da existência de pesquisa
$pesquisa = $_POST['pesquisa'];
if(!empty($_POST['pesquisa']))
{
// tabela onde vai ocorrer a pesquisa
$table = "mugidos";
// páginas adjacentes na numero de paginas de resultados
$adjacents = 1;
// explode as palavras colocadas na pesquisa em arrays
$arraySearch = explode(" ", $pesquisa);
// campos a pesquisar na tabela
$arrayFields = array(0 => "hashtag");
$countSearch = count($arraySearch);
$a = 0;
$b = 0;
$query = "SELECT * FROM ".$table." WHERE (";
$countFields = count($arrayFields);
while ($a < $countFields)
{
while ($b < $countSearch)
{
$query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
$b++;
if ($b < $countSearch)
{
$query = $query." AND ";
}
}
$b = 0;
$a++;
if ($a < $countFields)
{
$query = $query.") OR (";
}
}
$query = $query.")";
$query_result = mysql_query($query);
$numberrows = mysql_num_rows($query_result);
$pagenum= $numberrows / 4;
// resultados
echo '<h1>Resultados</h1>'."\n\n";
if($numberrows < 1)
{
echo '<p>Não foram encontrados resultados para "'.$pesquisa.'"</p>';
}
else
{
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
if($page)
{
$inicio = ($page - 1) * 10;
$fim = ($page * 10);
}
else
{
$inicio = 0;
$fim = 4;
}
$query = $query."LIMIT $inicio, $fim";
$query_result2 = mysql_query($query);
echo '<p>Resultados da pesquisa de "'.$pesquisa.'":</p>'."\n\n";
while($row = mysql_fetch_assoc($query_result2))
{
$remugido = $row['mugido'] ;
$repostador = $row['postador'] ;
$rehashtag = $row['hashtag'] ;
$rerepostador = $row['repostador'];
// aqui aparece o output da pesquisa
if($rerepostador === NULL)
{
echo "<table border='1' bordercolor='000000' style='background-color:FFFFFF' width='800' cellpadding='3' cellspacing='3'>";
echo "<TR><TD width='70%'>","\n";
echo "<strong>";
echo $repostador;
echo "</strong> ";
echo $remugido;
echo " <br><strong><font size='1'>";
echo $row['timestamp'];
echo "</strong></font>";
echo "<form action='retweet.php' method='post'>";
echo "<input type='hidden' value='";
echo $remugido;
echo "' name='remugido' style='height: 25px; width: 75px'><input type='submit' value='Re-Muuuu!' />";
echo "<input type='hidden' value='";
echo $repostador;
echo "' name='repostador' />";
echo "<input type='hidden' value='";
echo $rehashtag;
echo "' name='rehashtag' />";
echo "</form>";
echo "</TD>","\n";
echo "</TR></table>" ,"\n";
}
else
{
echo "<table border='1' bordercolor='000000' style='background-color:FFFFFF' width='800' cellpadding='3' cellspacing='3'>";
echo "<TR><TD width='70%'>","\n";
echo "<strong>";
echo $repostador;
echo "</strong> ";
echo " via ";
echo "<strong>";
echo $row['repostador'];
echo "</strong> ";
echo $remugido;
echo " <br><strong><font size='1'>";
echo $row['timestamp'];
echo "</strong></font>";
echo "<form action='retweet.php' method='post'>";
echo "<input type='hidden' value='";
echo $remugido;
echo "' name='remugido' style='height: 25px; width: 75px'><input type='submit' value='Re-Muuuu!' />";
echo "<input type='hidden' value='";
echo $repostador;
echo "' name='repostador' />";
echo "<input type='hidden' value='";
echo $rehashtag;
echo "' name='rehashtag' />";
echo "</form>";
echo "</TD>","\n";
echo "</TR></table>" ,"\n";
}
}
echo "<form method='post'><a href='{$_SERVER['PHP_SELF']}?pagenum=$pagenum'>";
echo "<input type='hidden' value='";
echo $pesquisa;
echo "' name='pesquisa'><INPUT TYPE='submit' VALUE='Proxima pagina'></a></form>";
}
}
else
{
echo ("Insira pelo menos um termo de pesquisa.<br />Redirecionando em 3 segundos.");
header ("Refresh: 3; url=page5.php");
die();
}
?>
</center>
</body>
</html>
答案 0 :(得分:1)
更改
$pesquisa = $_POST['pesquisa'];
到
$pesquisa = (isset($_POST['pesquisa'])) ? $_POST['pesquisa'] : '';
// or
$pesquisa = @$_POST['pesquisa']; // suppress warning if not set
// and then change
if(!empty($_POST['pesquisa']))
// to
if(trim($pesquisa) != ''))
您正在尝试分配$_POST['pesquisa'];
,但只有填写了搜索表单才会设置该值。
同样改变:
echo "<form method='post'><a href='{$_SERVER['PHP_SELF']}?pagenum=$pagenum'>";
echo "<input type='hidden' value='";
echo $pesquisa;
echo "' name='pesquisa'><INPUT TYPE='submit' VALUE='Proxima pagina'></a></form>";
到
echo "<form method='post' action='{$_SERVER['PHP_SELF']}?pagenum=$pagenum'>";
echo "<input type='hidden' value='";
echo htmlspecialchars($pesquisa);
echo "' name='pesquisa'><INPUT TYPE='submit' VALUE='Proxima pagina'></form>";
答案 1 :(得分:0)
这是不正确的:
echo "<form method='post'><a href='{$_SERVER['PHP_SELF']}?pagenum=$pagenum'>";
echo "<input type='hidden' value='";
echo $pesquisa;
echo "' name='pesquisa'><INPUT TYPE='submit' VALUE='Proxima pagina'></a></form>";
您将input
元素放在a
标记内。这是错的,您需要使用页码向表单添加另一个隐藏变量,以便在重新发布表单时获取它。
类似的东西:
echo "<form method='post'>";
echo "<input type='hidden' name='pagenum' value='" . (intval($_POST['pagenum']) + 1) . "' />";
echo "<input type='hidden' value='";
echo $pesquisa;
echo "' name='pesquisa'><INPUT TYPE='submit' VALUE='Proxima pagina'></form>";
之后显然你必须获得pagenum
的值并调整查询中的limit子句以适应它。
编辑:我第一次错过了,你正在使用:
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
所以你需要在底部的表格中使用变量$page
,而不是$pagenum
(我使用post,但它与get相同):
$page = (isset($_POST['page'])) ? (int)$_POST['page'] : 0;
...
echo "<form method='post'>";
echo "<input type='hidden' name='page' value='" . (intval($_POST['page']) + 1) . "' />";
echo "<input type='hidden' value='";
echo $pesquisa;
echo "' name='pesquisa'><INPUT TYPE='submit' VALUE='Proxima pagina'></form>";