Dynamic search like php and SQL Server does not bring results?

Asked

Viewed 48 times

0

I’m having trouble with one part of the code...

I have 5 fields to be searched with input, and they make a call in the database and complete with the respective data...

But among these 5 fields , 2 are not bringing any results!! Please help me !!!

Select code with table echo:

$sql = "SELECT 
    numero AS NUMERO,
    parcli.descricao AS DESCRIÇÃO,
    (SELECT top 1 db.cli.descricao
        FROM db.cli, db.out 
        WHERE outseq = prosequen
            AND outcliseq = clisequen
        ORDER BY outposica desc
    ) as OUTRA_PARTE,
    ISNULL(
        (SELECT db.cli.descricao + ' / ' AS [text()] FROM db.cli, db.PROPRA WHERE PRAPROSEQ = PROSEQUEN AND PRAADVSEQ = db.cli.CLISEQUEN AND db.cli.CLIADVTIP = 1 ORDER BY db.cli.descricao FOR XML PATH('')),' -'
    ) AS ADVOGADOS,
    ISNULL(ctrdescri ,'-') as CONTRATO
    FROM db.propro
        LEFT JOIN db.proprc on prcproseq = prosequen
        LEFT JOIN db.cli parcli on prcclnseq = parcli.clisequen 
        LEFT JOIN db.timctr on prcctrseq = ctrsequen
    WHERE protiposs = 1
        AND proencerr = 2
        AND numero like '%".utf8_decode($_POST['numero']). "%'
        AND descricao like '%".utf8_decode($_POST['parte'])."%'
        AND descricao like '%".utf8_decode($_POST['outra'])."%'
        AND descricao like '%".utf8_decode($_POST['nome'])."%'
        AND ctrdescri like '%".utf8_decode($_POST['contrato'])."%' 
    ";

   $stmt = sqlsrv_query( $conexion, $sql );

-

while($escrever=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)){           

    echo'<tr>
        <td style="font-family: Arial, sans-serif; font-size: 14px; padding: 10px 5px; overflow: hidden; word-break: normal; color: #333; background-color: #f0f0f0; ; vertical-align: top; border: 1px solid #aaa;">'.utf8_encode($escrever[0]).'</td>
        <td style="font-family: Arial, sans-serif; font-size: 14px; padding: 10px 5px; background-color: #f0f0f0; ; vertical-align: top; border: 1px solid #aaa; width: 14%;">'.utf8_encode($escrever[1]).'</td>
        <td style="font-family: Arial, sans-serif; font-size: 14px; padding: 10px 5px; overflow: hidden; word-break: normal; color: #333; background-color: #f0f0f0; ; vertical-align: top; border: 1px solid #aaa;">'.utf8_encode($escrever[2]).'</td>
        <td style="font-family: Arial, sans-serif; font-size: 14px; padding: 10px 5px; overflow: hidden; word-break: normal; color: #333; background-color: #f0f0f0; ; vertical-align: top; border: 1px solid #aaa;">'.utf8_encode($escrever[3]).'</td>
        <td style="font-family: Arial, sans-serif; font-size: 14px; padding: 10px 5px; overflow: hidden; word-break: normal; color: #333; background-color: #f0f0f0; ; vertical-align: top; border: 1px solid #aaa;">'.utf8_encode($escrever[4]).'</td>

        </tr>';

    }
}

" In the "Name" and "other" fields if I search in one of the two, it looks in the "part" column, so it is "lost"...

Obs: in table names are fictitious!

  • Could you tell which table each of the columns of the WHERE clause is in? // If you change the code by adding alias for each table, and using this alias to identify the columns, facilitates understanding of the code by third parties.

  • You should build the WHERE clause dynamically. Only restrict input fields that have been informed.

  • "number" is in the table "number" and "description" is in the table "parcli"

  • I got it, I just changed the variable where you do like , by the select of each column. So, it became dynamic ! You can close...

No answers

Browser other questions tagged

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