PHP filter and listing field

Asked

Viewed 22 times

0

I would like to list the data filtering by the form fields and if nothing is sent, I would like to not display results.

php.

<div class="row container">
    <div class="col s12">
        <h5 class="light">Gravações</h5><hr>
        
        <form action="BD/read-gravacoes.php" method="post" class="col s12">
            <fieldset class="formulario" style="padding: 15px">
                <!-- CAMPO NOME -->
                <div class="input-field col s12">
                    <i class="material-icons prefix">phone</i>
                    <input type="text" name="telefone" id="telefone" maxlength="40" autofocus>
                    <label for="telefone">Telefone</label>
                </div>
                <!-- BOTÕES -->
                <div class="input-field col s12">
                    <input type="submit" value="Pesquisar" class="btn blue">
                    <input type="reset" value="limpar" class="btn red">
                </div>
            </fieldset>
        </form>

        <table class="striped">
            <thead>
                <tr>
                    <th>Nome</th>
                    <th>Email</th>
                    <th>Telefone</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                    include_once 'BD/read-gravacoes.php';
                ?>
            </tbody>
        </table>
    </div>
</div>

read-write.php

<?php
include_once 'conexao.php';

$telefone     = filter_input(INPUT_POST, 'telefone', FILTER_SANITIZE_SPECIAL_CHARS);
$inicio     = filter_input(INPUT_POST, 'inicio', FILTER_SANITIZE_SPECIAL_CHARS);
$fim     = filter_input(INPUT_POST, 'fim', FILTER_SANITIZE_SPECIAL_CHARS);

$querySelect = $link->query("select * from tb_gravacoes");

if(isset($telefone)){
    $querySelect .= " WHERE telefone = $telefone";
}
elseif(isset($inicio)){
    $querySelect .= " WHERE data >= $inicio AND data <= $fim";
}
else {
    $querySelect .= " WHERE id = 0";
}


while($registros = $querySelect->fetch_assoc()):
    $id       = $registros['id'];
    $fila     = $registros['fila'];
    $telefone = $registros['telefone'];
    $gravacao = $registros['gravacao'];

    echo "<tr>";
    echo "
    <td>$fila</td>
    <td>$telefone</td>
    <td>$gravacao</td>
    ";
    echo "</tr>";

endwhile;

When I click on search, I get the return of the second image.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

1


When no search value is passed, you are trying to concatenate a string with an object mysqli_result.

try to use the code this way:

<?php
include_once 'conexao.php';

$telefone     = filter_input(INPUT_POST, 'telefone', FILTER_SANITIZE_SPECIAL_CHARS);
$inicio     = filter_input(INPUT_POST, 'inicio', FILTER_SANITIZE_SPECIAL_CHARS);
$fim     = filter_input(INPUT_POST, 'fim', FILTER_SANITIZE_SPECIAL_CHARS);

$raw_query = "select * from tb_gravacoes";

if(isset($telefone)){
    $raw_query .= " WHERE telefone = $telefone";
}
elseif(isset($inicio)){
    $raw_query .= " WHERE data >= $inicio AND data <= $fim";
}
else {
    $raw_query .= " WHERE id = 0";
}

$querySelect = $link->query($raw_query);

while($registros = $querySelect->fetch_assoc()):
    $id       = $registros['id'];
    $fila     = $registros['fila'];
    $telefone = $registros['telefone'];
    $gravacao = $registros['gravacao'];

    echo "<tr>";
    echo "
    <td>$fila</td>
    <td>$telefone</td>
    <td>$gravacao</td>
    ";
    echo "</tr>";

endwhile;

Browser other questions tagged

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