Problems in searching for data by name, does not return anything

0

I'm having trouble fetching data by name. The following is the code. Thanks in advance.

<?php include_once 'dbconfig.php'; ?>
<?php include_once 'header3.php'; ?>

<div class="clearfix"></div>

<div class="container" style=" background-color:#69C">
    <legend style="color:#FFF" align="center"><h2>Resultado da Busca por Nome</h2></legend>
    <form action="" method="get" id='form-contato' class="form-horizontal col-md-10">
        <label class="col-md-2 control-label" for="termo" style="color:#FFFFFF">Pesquisar</label>
        <div class='col-md-7'>
            <input type="text" class="form-control" id="nome" name="nome" placeholder="Infome o Nome">
        </div>
        <button type="submit" class="btn btn-primary">Pesquisar</button>
        <a href='index.php' class="btn btn-primary">Ver Todos</a>
    </form>

 </div><!--container-->

<div class="clearfix"></div><br />

<div class="container">

    <table class='table table-hover table-border table-responsive'>
        <tr bgcolor="#99CCFF">
            <th>#</th>
            <th>Nome</th>
            <th>Sobrenome</th>
            <th>Email</th>
            <th>Telefone</th>
            <th colspan="2" align="center">Ação</th>            
        </tr>
        <?php
            $nome=(isset($_GET['nome']));
            if(!empty($nome))
            {
                $sql = "SELECT * FROM dbpdo.tbl_usuarios WHERE nome LIKE :nome OR email LIKE :email";
                $stm = $DB_con->prepare($sql);
                $stm->bindValue(":nome", $nome);
                $stm->bindValue(":email", $nome);
                $stm->execute();

                if($stm->rowCount()>0)
                {
                    while($row=$stm->fetchAll(PDO::FETCH_OBJ))
                    {
                        ?>
                            <tr>
                            <td><?php print($row['id']); ?></td>
                            <td><?php print($row['nome']); ?></td>
                            <td><?php print($row['sobrenome']); ?></td>
                            <td><?php print($row['email']); ?></td>
                            <td><?php print($row['telefone']); ?></td>
                            <td align="center">
                            <a href="edit-data.php?edit_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-edit"></i></a>
                            </td>
                            <td align="center">
                            <a href="delete.php?delete_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-trash"></i></a>
                            </td>
                            </tr>
                        <?php
                    }
                }
                else
                {
                    ?>
                        <tr>
                            <td>Não existem dados para visualizar!</td>
                        </tr>
                    <?php
                }
            }       

        ?>        

    </table>

</div><!--container-->

<?php include_once 'footer.php';?>
    
asked by anonymous 29.03.2017 / 11:48

1 answer

2
Hello, I made some changes to your file, when you check the isset and assign it to a variable you are assigning a Boolean value (true / false) so I think it might be that. Another thing, when you use PDO::FETCH_OBJ you have to retrieve the same as an object and not a vector. Examples:

  

Accessing an object's attribute:

$row->id
  

Accessing a vector value (array):

$row['id']
  

Note: Also try to check if your connection to the database is right, assuming you select a default database on your connection, I took the liberty of removing the database name (dbpdo.) in your SQL.

I leave below the modifications that I believe should be made for your script to work.

<?php include_once 'dbconfig.php'; ?>
<?php include_once 'header3.php'; ?>

<div class="clearfix"></div>

<div class="container" style=" background-color:#69C">
    <legend style="color:#FFF" align="center"><h2>Resultado da Busca por Nome</h2></legend>
    <form action="" method="get" id='form-contato' class="form-horizontal col-md-10">
        <label class="col-md-2 control-label" for="termo" style="color:#FFFFFF">Pesquisar</label>
        <div class='col-md-7'>
            <input type="text" class="form-control" id="nome" name="nome" placeholder="Infome o Nome">
        </div>
        <button type="submit" class="btn btn-primary">Pesquisar</button>
        <a href='index.php' class="btn btn-primary">Ver Todos</a>
    </form>

 </div><!--container-->

<div class="clearfix"></div><br />

<div class="container">

    <table class='table table-hover table-border table-responsive'>
        <tr bgcolor="#99CCFF">
            <th>#</th>
            <th>Nome</th>
            <th>Sobrenome</th>
            <th>Email</th>
            <th>Telefone</th>
            <th colspan="2" align="center">Ação</th>            
        </tr>
        <?php
            $nome = isset($_GET['nome']) ? $_GET['nome'] : '';
            if(!empty($nome))
            {

                $nome = '%'.$nome.'%';

                $sql = "SELECT * FROM tbl_usuarios WHERE nome LIKE :nome OR email LIKE :email";
                $stm = $DB_con->prepare($sql);
                $stm->bindValue(":nome", $nome);
                $stm->bindValue(":email", $nome);
                $stm->execute();

                if($stm->rowCount()>0)
                {
                    while($row=$stm->fetchAll(PDO::FETCH_OBJ))
                    {
                        ?>
                            <tr>
                            <td><?=$row->id?></td>
                            <td><?=$row->nome?></td>
                            <td><?=$row->sobrenome?></td>
                            <td><?=$row->email?></td>
                            <td><?=$row->telefone?></td>
                            <td align="center">
                            <a href="edit-data.php?edit_id=<?=$row->id?>"><i class="glyphicon glyphicon-edit"></i></a>
                            </td>
                            <td align="center">
                            <a href="delete.php?delete_id=<?=$row->id?>"><i class="glyphicon glyphicon-trash"></i></a>
                            </td>
                            </tr>
                        <?php
                    }
                }
                else
                {
                    ?>
                        <tr>
                            <td>Não existem dados para visualizar!</td>
                        </tr>
                    <?php
                }
            }       

        ?>        

    </table>

</div><!--container-->

<?php include_once 'footer.php';?>
    
29.03.2017 / 14:06