Problem with the Code

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Problem with the Code

Postby multipianista » 24. January 2014 23:54

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>
multipianista
 
Posts: 10
Joined: 24. January 2014 23:51
Operating System: Windows 8

Re: Problem with the Code

Postby Altrea » 25. January 2014 13:44

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
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Problem with the Code

Postby multipianista » 28. January 2014 20:10

Thanks, it worked.
multipianista
 
Posts: 10
Joined: 24. January 2014 23:51
Operating System: Windows 8


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 114 guests