I am using the code show below for a search and it is working. But when I try it to display another page with other results it says that “the index is undefined". I'm using again in this file but it does not solve anything. Can anyone tell me why is this happening?
The error is:
Notice: Undefined index: pesquisa in J:\xampp\htdocs\pesquisa3.php on line 32
The code is:
<?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>