Problem with SQL query

Asked

Viewed 80 times

0

I’m trying to make a query but return me the values selected in a select and I can’t because it returns all the entered values. Can someone please help me?

<form id="form1" name="form1" method="post" action="consultar_documento.php">
Selecionar Tipo de Documento
<select name="COMBO" id="COMBO">
<option></option>
<?php    
$botao   = $_REQUEST['BT'];
$server  = 'localhost';
$DBName  = 'arquivo';
$senha   = '';
$usuario = 'root';

$con12=mysql_connect($server,$usuario,$senha);
if($con12) {
  $selecionar=mysql_select_db($DBName,$con12);
  if($selecionar) {
    $consulta="SELECT descricao FROM tipo_de_documento";
    $resultado=mysql_query($consulta,$con12);

    while ($Reg=mysql_fetch_row($resultado)) {
      foreach ($Reg as $Val) {
        echo "<option>$Val</option>";
      }
    }

    mysql_close($con12);
  }
}
?>  

</select> 
<input type="submit" name="BT" id="BT" value="Consultar" />

</form> 

<?php 
$botao=$_REQUEST['BT'];
$server='localhost';
$DBName='arquivo';
$senha='';
$usuario='root';

$con12 = mysql_connect($server,$usuario,$senha);   
if($con12) {
  $selecionar=mysql_select_db($DBName,$con12);
  if($selecionar) {
    if($botao=='Consultar') {
      if ((isset($_POST['interno'])) && (!empty($_POST['externo']))) {
        $consulta = "SELECT tipo_de_documento.id_tipodocumento, tipo_de_documento.descricao, 
        tipo_de_documento.Data_entrada, tipo_de_documento.Data_saida, tipo_de_documento.UEO, 
        documento.assunto, documento.n_doc, documento.n_folhas, documento.n_exemplares, 
        documento.iddocumento, tipo_de_documento.id_tipodocumento 
        FROM documento, tipo_de_documento 
        WHERE tipo_de_documento.descricao = '".$_POST['tipo_de_documento']."'";
      }

      $resultado=mysql_query($consulta,$con12);

      echo "<table border='1' align='center' width='900' bgcolor='#FFFFFF'> ";
      echo "<tr>"; 
      echo "<table border='1' align='center' width='900' bgcolor='#FFFFFF'> ";  
      echo "<tr>"; 
      echo "<th colspan='9'>";
      echo 'documento';echo $COMBO;
      echo "</th>"; 
      echo "</tr>";
      echo "<tr>";
      echo "<th>";
      echo 'IDENTIFICADOR DE DOCUMENTO';
      echo "</th>";
      echo "<th>";
      echo 'DESCRICAO';
      echo "</th>";
      echo "<th>";
      echo 'DATA_ENTRADA';
      echo "</th>";
      echo "<th>";
      echo 'DATA_SAIDA';
      echo "</th>";
      echo "<th>";
      echo 'UEO';
      echo "</th>";
      echo "<th>";
      echo 'ASSUNTO';
      echo "</th>";
      echo "<th>";
      echo 'NUMERO DO DOCUMENTO';
      echo "</th>"; 
      echo "<th>";
      echo 'NUMERO DE FOLHAS';
      echo "</th>";
      echo "<th>";
      echo 'NUMERO DE EXEMPLARES';
      echo "</th>";
      echo "<th>";


      echo mysql_num_rows($resultado) or die ("".mysql_error ());

      while ($Reg=mysql_fetch_row($resultado)) {
        echo "<tr>";

        foreach ($Reg as $Val) {
          echo "<td>$Val</td> ";
        }
        echo "</tr>";
      }
      echo "</table>";

      mysql_close($con12);
    }
  }
}
?>
</td>
</tr>
</table>
  • 1

    Shouldn’t have a where somewhere?

  • What method do you use in <form> and what the name of <select>?

  • in the form use the post method

2 answers

2

Dude, in this case your SELECT will bring everything even, if you want a certain value you need to insert the WHERE in the variable $query, you need to link the two tables within the WHERE clause, you repeat the document type ID in but document type table twice, I believe that one of them should be the column ID_TIPODOCUMENTO of the table 'document', right? Having this foreign key you will need to link it in the Where clause and separate by any other condition you wish followed by AND: Example:

FROM DOCUMENTO, TIPO_DE_DOCUMENTO
WHERE DOCUMENTO.ID_TIPODOCUMENTO = TIPO_DE_DOCUMENTO.ID_TIPODOCUMENTO
AND DOCUMENTO.ASSUNTO = 'terça-feira';
  • I am making this query because in the table there are 3 types of documents internal, external and hybridized, what I want is that when I click one of them shows me all the data only of the selected document. What does not happen because the query shows me the data of the 3 types of documents at once...

1


Your SQL should look like this:

if ((isset($_POST['tipo_de_documento'])) && (!empty($_POST['tipo_de_documento']))) {
    $consulta = "SELECT tipo_de_documento.id_tipodocumento, tipo_de_documento.descricao, 
    tipo_de_documento.Data_entrada, tipo_de_documento.Data_saida, tipo_de_documento.UEO, 
    documento.assunto, documento.n_doc, documento.n_folhas, documento.n_exemplares, 
    documento.iddocumento, tipo_de_documento.id_tipodocumento 
    FROM documento, tipo_de_documento 
    WHERE tipo_de_documento.descricao = '".$_POST['tipo_de_documento']."'";
}

Where is $_POST['tipo_de_documento'] alter tipo_de_documento to the name who is in the <select>.

  • thanks, so when I change my code appears syntax error on this line if (isset($_POST['typo_de_document']) && (!Empty($_POST['typo_de_document'])) { and I’m not sure how to fix...but a help there please

  • I fixed the code. Some parentheses are missing. Try again.

  • hello tried again with the code fixed and now the query does not return nor a value

  • Is there any way you can get the full code into jsFiddle?

  • I sent you the complete code

  • Sent by where?

  • ups can’t send can post even here?

  • Edit your question by placing the whole code.

  • Man, I told you to change $_POST['tipo_de_documento'] changing the tipo_de_documento by the name of his select. The name of your select is not tipo_de_documento is COMBO. will stay $_POST['COMBO']

  • I changed is working properly, thank you mainly for the patience.You saved my day and many days fustrados.Thanks you are maisss

  • Click on the check next to my reply to mark how you accept.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.