Data search problems by name, returns nothing

Asked

Viewed 55 times

0

I’m having trouble searching for data by name. Following is the code. From now on, thank you.

<?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';?>
  • What’s the problem? Syntax error, returns only a part, returns nothing?

  • Returns nothing.

  • Daniel, take advantage and put in the description of your problem what you told me. What is the value of $nome? Print your value on screen

  • The value of $name is taken from the $_GET['name method'].

  • Yes, I know. What’s the content? That’s why I asked you to print it on the screen. Maybe like a header <h1> to get really big

  • OK. The value of $name is 1, whatever name you put in the search.

  • So I guess you don’t have anyone with name 1 or email 1. Emails need @, and name 1 doesn’t make sense.

  • That’s right! And how do I make the value of $name whatever I type in the search?

  • That’s from form html that makes a life that I do not move, barely there. Try to access by passing the value in the browser, passing the query param in a controlled manner: 127.0.0.1/pagina.php?nome=jeff

  • Passing the value in the browser, continues in the same, even because even passing the value in the search, also see the in the browser.

  • 2

    I left my reply, the value is one because the isset returned true! he did not assign the value of $_GET['name'] and yes the return of isset($_GET['name']) which is "true or 1" because the value was passed in $_GET...

Show 6 more comments

1 answer

2


Hello I made some changes to your file when you check the isset and assigns it to a variable you are assigning a boolean value (true/false) so I believe that might be it. Something else, when you use the PDO::FETCH_OBJ you have to retrieve it as an object and not a vector. Examples:

Accessing attribute of an object:

$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';?>
  • Thank you very much brother! Solved my problem.

  • @Danieldossantos Opa we are here for this.

Browser other questions tagged

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