Doubt IF with SQL

Asked

Viewed 75 times

-2

It is calling only an if option.

1 = >all users by date = if empty user and date filled (only this calling and msm so a user has q be selected, what is wrong the user had to be empty)

2 = > user per date = user completed and date completed

3 = >only the user = completed user and empty date. Everything else returns nothing.

the code is like this now:

if (isset($_POST['busca']) && !empty($_POST['busca'])){
  //todos usuarios por periodo
  $sql = "SELECT * FROM assentamentos a, usuarios u, ocorrencias o, problemas p, localizacao l, instituicao i, sistemas s WHERE o.data_abertura BETWEEN '$ymdinicio' AND '$ymdfim' and a.responsavel = u.user_id and a.data = o.data_fechamento and p.prob_id = o.problema and o.local = l.loc_id and i.inst_cod = o.instituicao and s.sis_id = o.sistema ORDER BY o.numero DESC";

        }
        else if(isset($_POST['data_inicial']) && !empty($_POST['data_inicial']) and isset($_POST['data_final']) && !empty($_POST['data_final'])){


    //so usuario 

    $sql = "SELECT * FROM assentamentos a, usuarios u, ocorrencias o, problemas p, localizacao l, instituicao i, sistemas s WHERE u.nome = '$busca' and a.responsavel = u.user_id and a.data = o.data_fechamento and p.prob_id = o.problema and o.local = l.loc_id and i.inst_cod = o.instituicao and s.sis_id = o.sistema ORDER BY o.numero DESC";

    }
    else{

    //usuario por periodo
    $sql = "SELECT * FROM assentamentos a, usuarios u, ocorrencias o, problemas p, localizacao l, instituicao i, sistemas s WHERE o.data_abertura BETWEEN '$ymdinicio' AND '$ymdfim' and u.nome = '$busca' and a.responsavel = u.user_id and a.data = o.data_fechamento and p.prob_id = o.problema and o.local = l.loc_id and i.inst_cod = o.instituicao and s.sis_id = o.sistema ORDER BY o.numero DESC";
    }

         print "<table width='100%' border='0' align='center' cellpadding='1' cellspacing='2' class='full_table_list'>";
         print "<tr class='tabela_cinza'>";
         print "<td align='center' colspan='10'><span class='texto_negrito'>Relat&oacute;rio Geral</span></td>";
         print "</tr>";
         print "<tr>";
         print "<td align='center' width='8%'><span class='texto_negrito'>Número</span></td>";
         print "<td align='center' width='8%'><span class='texto_negrito'>Técnico</span></td>";
         print "<td align='center' width='8%'><span class='texto_negrito'>Problema</span></td>";
         print "<td align='center' width='16%'><span class='texto_negrito'>Descriçao do problema</span></td>";
         print "<td align='center' width='16%'><span class='texto_negrito'>Soluçao do Problema</span></td>";
         print "<td align='center' width='8%'><span class='texto_negrito'>Área</span></td>";
         print "<td align='center' width='8%'><span class='texto_negrito'>Unidade</span></td>";
         print "<td align='center' width='12%'><span class='texto_negrito'>Local</span></td>";
         print "<td align='center' width='8%'><span class='texto_negrito'>Data inicial</span></td>";
         print "<td align='center' width='8%'><span class='texto_negrito'>Data final</span></td>";
         print "</tr>";
         print "</table>";
          $qr = mysql_query($sql) or die(mysql_error());
         while ($linha = mysql_fetch_array($qr)) {
         print "<form action='' method='post' name='CadInfo' target='_parent' id='CadInfo'>";
         print "<table width='100%' border='0' align='center' cellpadding='1' cellspacing='2' class='full_table_list'>";
         print "<tr>";
         print "<td align='center' width='8%'><span class='texto_center'>".$linha['numero']."</span></td>";
         print "<td align='center' width='8%'><span class='texto_center'>".$linha['nome']."</span></td>";
         print "<td align='center' width='8%'><span class='texto_center'>".$linha['problema']."</span></td>";
         print "<td align='center' width='16%'><span class='texto_center'>".$linha['descricao']."</span></td>";
         print "<td align='center' width='16%'><span class='texto_center'>".$linha['assentamento']."</span></td>";
         print "<td align='center' width='8%'><span class='texto_center'>".$linha['sistema']."</span></td>";
         print "<td align='center' width='8%'><span class='texto_center'>".$linha['inst_nome']."</span></td>";
         print "<td align='center' width='12%'><span class='texto_center'>".$linha['local']."</span></td>";
         print "<td align='center' width='8%'><span class='texto_center'>".$linha['data_abertura']."</span></td>";
         print "<td align='center' width='8%'><span class='texto_center'>".$linha['data_fechamento']."</span></td>";
         print "</tr>";
         print "</table>";
         print "</form>";
         }

A great weekend for everyone.

  • The blocks , if, else if e else are mutually exclusive or when entering one the others are ignored. Only in the else the query is executed and iterated, seems to be the problem.

  • I don’t have much knowledge, but I think the problem is that in the middle of the if you used the and, unlike a &&.

  • 1

    The operator and exists but is different from &&, follows documentation: Operators

  • These are 3 query options that I have to check, I used if pq it only has to run 1 of them: 1 query only by the user 2 query the user by date 3 query all users by date

  • In two options the mysql_query() and the mysql_fetch_assoc()

  • could give me an example of how it should be?

Show 1 more comment

1 answer

0


You’d have to do it this way:

if ($_POST['busca'] == '-- Selecione o Tecnico --'){
//todos usuarios por periodo
$sql = "SELECT * FROM assentamentos a, usuarios u, ocorrencias o, problemas p, localizacao l, instituicao i, sistemas s WHERE o.data_abertura BETWEEN '$ymdinicio' AND '$ymdfim' and a.responsavel = u.user_id and a.data = o.data_fechamento and p.prob_id = o.problema and o.local = l.loc_id and i.inst_cod = o.instituicao and s.sis_id = o.sistema ORDER BY o.numero DESC";

} else if ((isset($_POST['data_inicial']) && !empty($_POST['data_inicial'])) && (isset($_POST['data_final']) && !empty($_POST['data_final']))){

//so usuario 
$sql = "SELECT * FROM assentamentos a, usuarios u, ocorrencias o, problemas p, localizacao l, instituicao i, sistemas s WHERE u.nome = '$busca' and a.responsavel = u.user_id and a.data = o.data_fechamento and p.prob_id = o.problema and o.local = l.loc_id and i.inst_cod = o.instituicao and s.sis_id = o.sistema ORDER BY o.numero DESC";

} else {

//usuario por periodo
$sql = "SELECT * FROM assentamentos a, usuarios u, ocorrencias o, problemas p, localizacao l, instituicao i, sistemas s WHERE o.data_abertura BETWEEN '$ymdinicio' AND '$ymdfim' and u.nome = '$busca' and a.responsavel = u.user_id and a.data = o.data_fechamento and p.prob_id = o.problema and o.local = l.loc_id and i.inst_cod = o.instituicao and s.sis_id = o.sistema ORDER BY o.numero DESC";
}


 print "<table width='100%' border='0' align='center' cellpadding='1' cellspacing='2' class='full_table_list'>";
 print "<tr class='tabela_cinza'>";
 print "<td align='center' colspan='10'><span class='texto_negrito'>Relatório Geral</span></td>";
 print "</tr>";
 print "<tr>";
 print "<td align='center' width='8%'><span class='texto_negrito'>Número</span></td>";
 print "<td align='center' width='8%'><span class='texto_negrito'>Técnico</span></td>";
 print "<td align='center' width='8%'><span class='texto_negrito'>Problema</span></td>";
 print "<td align='center' width='16%'><span class='texto_negrito'>Descriçao do problema</span></td>";
 print "<td align='center' width='16%'><span class='texto_negrito'>Soluçao do Problema</span></td>";
 print "<td align='center' width='8%'><span class='texto_negrito'>Área</span></td>";
 print "<td align='center' width='8%'><span class='texto_negrito'>Unidade</span></td>";
 print "<td align='center' width='12%'><span class='texto_negrito'>Local</span></td>";
 print "<td align='center' width='8%'><span class='texto_negrito'>Data inicial</span></td>";
 print "<td align='center' width='8%'><span class='texto_negrito'>Data final</span></td>";
 print "</tr>";
 print "</table>";
     // $qr = mysql_query($sql, $sql2, $sql3) or die(mysql_error());
 while ($linha = mysql_fetch_array($qr)) {
    print "<form action='' method='post' name='CadInfo' target='_parent' id='CadInfo'>";
    print "<table width='100%' border='0' align='center' cellpadding='1' cellspacing='2' class='full_table_list'>";
    print "<tr>";
    print "<td align='center' width='8%'><span class='texto_center'>".$linha['numero']."</span></td>";
    print "<td align='center' width='8%'><span class='texto_center'>".$linha['nome']."</span></td>";
    print "<td align='center' width='8%'><span class='texto_center'>".$linha['problema']."</span></td>";
    print "<td align='center' width='16%'><span class='texto_center'>".$linha['descricao']."</span></td>";
    print "<td align='center' width='16%'><span class='texto_center'>".$linha['assentamento']."</span></td>";
    print "<td align='center' width='8%'><span class='texto_center'>".$linha['sistema']."</span></td>";
    print "<td align='center' width='8%'><span class='texto_center'>".$linha['inst_nome']."</span></td>";
    print "<td align='center' width='12%'><span class='texto_center'>".$linha['local']."</span></td>";
    print "<td align='center' width='8%'><span class='texto_center'>".$linha['data_abertura']."</span></td>";
    print "<td align='center' width='8%'><span class='texto_center'>".$linha['data_fechamento']."</span></td>";
    print "</tr>";
    print "</table>";
    print "</form>";
 }

Note that the printing of the results should be outside of if Else, otherwise it will only print in that context.

  • vlw! it is calling one of the options I need to see now where I am missing in the other 2...

  • It is calling only an if option. 1 = >all users by date = if empty user and date filled (only this calling and msm so a user has q be selected, what is wrong the user had to be empty) 2 = > user by date = completed user and date filled 3 = >so the user = completed user and empty date everything else returns nothing.

  • I made a fix on the Else block if, make sure it works now. I noticed that Else if makes the condition if there is an initial and final date set. And in your description you inform that it would be to search for only one user. I believe to be reversed your conditions. Check this also there

Browser other questions tagged

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