Page 1 of 1

Problem with the Code

PostPosted: 24. January 2014 23:54
by multipianista
Hi, I have this problem:

"Notice: Undefined index: datos in C:\xampp\htdocs\codigo.php on line 11

Notice: Undefined index: datos in C:\xampp\htdocs\codigo.php on line 12"

And here is the code. "datos" is the name of the table, any idea where's the problem?:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
</head>

<body>
<?php
include("conexion.php");
$_POST["datos"];
$palabras = explode(" ",$_POST["datos"]);
?>
<form action="index.php" method="post">
Buscar: <input type="text" size="50" name="datos"><br><input type="submit" name="buscar" value="Enviar"></form><?php
if($_POST["datos"]){
$palabras = explode(" ",$_POST["datos"]);
$totalresults = count($palabras);
$datos = "";
for($i=0;$i < $totalresults;$i++){
$datos .= (($i > 0) ? " OR " : " WHERE ")."autor LIKE '%".mysql_real_escape_string($palabras[$i])."%' OR titulo LIKE '%".mysql_real_escape_string($palabras[$i])."%' OR paginas LIKE '%".mysql_real_escape_string($palabras[$i])."%' OR instrumentos LIKE '%".mysql_real_escape_string($palabras[$i])."%' OR detalles LIKE '%".mysql_real_escape_string($palabras[$i])."%'";
}
$sacacanciones = mysql_query("SELECT * FROM canciones ".$datos."");
if(mysql_num_rows($sacacanciones) > 0){
echo "<table><tr><td>Páginas</td><td>Instrumentos</td><td>Autor</td><td>Título</td><td>Partitura</td><td>Detalles</td></tr>";
while($c = mysql_fetch_array($sacacanciones)){
echo "<tr><td>".$c["paginas"]."</td><td>".$c["instrumentos"]."</td><td>".$c["autor"]."</td><td>".$c["titulo"]."</td><td><a href=\"".$c["enlace"]."\">Descargar</a></td><td>".$c["detalles"]."</td></tr>";
}
echo "</table>";
} else {
echo "No se ha encontrado ningún resultado que coincida con su búsqueda.";
}
} else {
$_POST["datos"] = "";
}
?>
</body>
</html>

Re: Problem with the Code

PostPosted: 25. January 2014 13:44
by Altrea
Hi,

$_POST["datos"] is not set unless the form is send.
So if you initially request the form you will get this notices.
To solve this make use of the php functions isset() or empty()

best wishes,
Altrea

Re: Problem with the Code

PostPosted: 28. January 2014 20:10
by multipianista
Thanks, it worked.