Advertisement
gastaojunior

exemplo de pesquisa

Nov 8th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.75 KB | None | 0 0
  1. <?php
  2.  
  3. include "conn.php";
  4.  
  5. //Verifico se chegou parâmetros de pesquisa
  6.  
  7. $titulo="";
  8. $autor="";
  9. $sql = "select titulo, autor, editora, ano, paginas, categoria from livro where 1=1 ";
  10.  
  11. if(isset($_GET['pesquisar'])){
  12.     $titulo=$_GET['titulo'];
  13.     $autor=$_GET['autor'];
  14.    
  15.     if(!empty($titulo)){
  16.         $sql.=" and titulo like '%$titulo%' ";
  17.     }
  18.    
  19.     if(!empty($autor)){
  20.         $sql.=" and autor like '%$autor%' ";
  21.     }
  22. }
  23.  
  24. //Busco os dados
  25.  
  26. $dados = mysql_query($sql) or die("Erro na pesquisa. Código: $SQL <br/> Erro do banco:". mysql_error());
  27.  
  28. ?>
  29. <html>
  30.     <body>
  31.    
  32.         <fieldset>
  33.             <legend>Pesquisar:</legend>
  34.                 <form name="pesquisa" action="" method="GET">
  35.                     Titulo: <input type="text" name="titulo" size="30" value="<?php echo $titulo;?>"/> <br/>
  36.                     Autor: <input type="text" name="autor" size="30" value="<?php echo $autor;?>"/><br/>
  37.                     <input type="submit" value="Pesquisar" name="pesquisar"/>
  38.                 </form>
  39.         </fieldset>
  40.         <hr/>
  41.         <table border="1" width="100%">
  42.             <tr>
  43.                 <td><b>Título</b></td>
  44.                 <td><b>Autor</b></td>
  45.                 <td><b>Editora</b></td>
  46.                 <td><b>Ano</b></td>
  47.                 <td><b>Páginas</b></td>
  48.                 <td><b>Categoria</b></td>
  49.                 <td>&nbsp;</td>
  50.             </tr>
  51.            
  52.             <?php
  53.             while ($row = mysql_fetch_array($dados, MYSQL_ASSOC)) {
  54.                 $titulo=$row['titulo'];
  55.                 $autor=$row['autor'];
  56.                 $editora=$row['editora'];
  57.                 $ano=$row['ano'];
  58.                 $paginas=$row['paginas'];
  59.                 $categoria=$row['categoria'];
  60.                 echo "
  61.                 <tr>
  62.                     <td>$titulo</td>
  63.                     <td>$autor</td>
  64.                     <td>$editora</td>
  65.                     <td>$ano</td>
  66.                     <td>$paginas</td>
  67.                     <td>$categoria</td>
  68.                     <td><img src='alterar.png' width='24' height='24'> <img src='deletar.png' width='24' height='24'></td>
  69.                 </tr>";
  70.             }
  71.             ?>
  72.            
  73.         </table>   
  74.    
  75.     </body>
  76. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement