PHP & SQL - How to get specific information from each user in a table

Asked

Viewed 86 times

1

Hello, I’m having trouble collecting information from users of a database.

In DB there are some users, and they are displayed in a table.

inserir a descrição da imagem aqui

My goal is that when the names are clicked, a Collapse occurs that shows all the information of the clicked user, but when clicked occurs the error:

Undefined index

Follows the code:

<div class="row" id="realTimeContents myGroup">
        <div class="col-md-8">
            <div class="scrollable">
                <table class="table table-striped table-dark text-center">
                    <thead>
                        <tr>
                            <td>ID</td>
                            <td>Nome</td>
                            <td>CPF/CNPJ</td>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            $sql = "SELECT * FROM registro";
                            $result = mysqli_query($conexao, $sql);
                            $queryResults = mysqli_num_rows ($result);

                            if ($queryResults > 0) {
                            while ($row = mysqli_fetch_assoc($result)) {
              echo "<tr>
                                <td scope='row'>".$row['id']."</td>
                                <td><a class='' data-toggle='collapse' aria-expanded='false' aria-controls='teste' href='#teste'?nome=".$row['nome']."&CPF=".$row['CPF']."'>".$row['nome']."</a></td>
                                <td>".$row['CPF']."</td>
                                </tr>";
                                }
                            }
                        ?>
                    </tbody>
        <!-- collpse user content -->
      <div class="collapse" id="teste">
        <?php
            $nome = mysqli_real_escape_string($conexao, $_GET['nome']);
            $CPF = mysqli_real_escape_string($conexao, $_GET['CPF']);

            $sql = "SELECT * FROM registro WHERE nome='$nome' AND CPF='$CPF'";
            $result = mysqli_query($conexao, $sql);
            $queryResults = mysqli_num_rows ($result);

            if ($queryResults > 0) {
              while ($row = mysqli_fetch_assoc($result)) {
                echo "<td>".$row['nome']."</td>
                    <td>".$row['CPF']."</td>
                    <td>".$row['data_de_nascimento']."</td>
                    <td>".$row['numero_de_cadastro']."</td>
                    <td>".$row['CEP']."</td>
                    <td>".$row['numero']."</td>
                    <td>".$row['bairro']."</td>
                    <td>".$row['endereço']."</td>
                    <td>".$row['cidade']."</td>
                    <td>".$row['complemento']."</td>
                    <td>".$row['uf']."</td>
                    <td>".$row['telefone_residencial']."</td>
                    <td>".$row['telefone_comercial']."</td>
                    <td>".$row['categoria_de_cliente']."</td>
                    <td>".$row['telefone_celular']."</td>
                    <td>".$row['telefone_0800']."</td>
                    <td>".$row['insc_estadual']."</td>
                    <td>".$row['CNPJ']."</td>
                    <td>".$row['RG']."</td>
                    <td>".$row['insc_municipal']."</td>
                    <td>".$row['PIS']."</td>
                    <td>".$row['site']."</td>
                    <td>".$row['email']."</td>
                    <td>".$row['historico']."</td>";
              }
            }
        ?>
      </div>
                </table>
            </div>
        </div>
    </div>

Table structure:

inserir a descrição da imagem aqui

The format I used to try to show all user information is experimental, no problem if there is a better way to do this process.

I’m sorry if you seemed confused, I tried to be as clear as I could be.

From now on, thank you for your patience!

  • unfortunately you will have to debug to find the problem I would try to var_dump this code starting with $Row!

  • It would be interesting if you used ajax, that the loading would be faster.

  • @I’ll try, thanks for the tip

  • @Viniciusdejesus I don’t know how to do it but I’ll see how it works ;D

2 answers

0


Good afternoon, I was too lazy to make a similar table, and I also have no affinity with mysqli, because I only use PDO,.

This is your user list:

<div class="row" id="realTimeContents myGroup">
    <div class="col-md-8">
        <div class="scrollable">
            <table class="table table-striped table-dark text-center">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td>Nome</td>
                        <td>CPF/CNPJ</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $sql = "SELECT id, nome, cpf FROM registro";
                    $result = mysqli_query($conexao, $sql);
                    $queryResults = mysqli_num_rows ($result);

                    if ($queryResults > 0) {
                        while ($row = mysqli_fetch_assoc($result)) {
                            ?>
                            <tr>
                                <td scope='row'><?= $row['id']; ?></td>
                                <td class= 'click' id="<?= $row['id']; ?>"><?= $row['nome']; ?></td>
                                    <td><?= $row['CPF']; ?></td>
                                </tr>
                                <div class='collapse' id='teste_<?= $row['id']; ?>'>

                                </div>

                                <?php
                                ;
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(".click").click(function(){
            var id = $(this).attr('id');
            $.ajax({
                type: "POST",
                url: ajax.php,
                data: $(this).serialize(),
                success: function(data){
                    $("#teste_" + id).empty();
                    $("#teste_" + id).append(data);
                }
            });
        });
    </script>

and this ajax.php would be the page that runs the search for the rest of the information and would play in #test_id:

<?php
$id = $_POST['id'];

$sql = "SELECT id, nome FROM registro where id =" . $id;
$result = mysqli_query($conexao, $sql);
$row = mysqli_fecth_array ($result);
//adapte o sql pra pq eu sei usar o mysqli então deve ter erro de sintaxe
?>
<td><?= $row['nome']; ?>  </td>
<td><?= $row['CPF']; ?>  </td>
<td><?= $row['data_de_nascimento']; ?>  </td>
<td><?= $row['numero_de_cadastro']; ?>  </td>
<td><?= $row['CEP']; ?>  </td>
<td><?= $row['numero']; ?>  </td>
<td><?= $row['bairro']; ?>  </td>
<td><?= $row['endereço']; ?>  </td>
<td><?= $row['cidade']; ?>  </td>
<td><?= $row['complemento']; ?>  </td>
<td><?= $row['uf']; ?>  </td>
<td><?= $row['telefone_residencial']; ?>  </td>
<td><?= $row['telefone_comercial']; ?>  </td>
<td><?= $row['categoria_de_cliente']; ?>  </td>
<td><?= $row['telefone_celular']; ?>  </td>
<td><?= $row['telefone_0800']; ?>  </td>
<td><?= $row['insc_estadual']; ?>  </td>
<td><?= $row['CNPJ']; ?>  </td>
<td><?= $row['RG']; ?>  </td>
<td><?= $row['insc_municipal']; ?>  </td>
<td><?= $row['PIS']; ?>  </td>
<td><?= $row['site']; ?>  </td>
<td><?= $row['email']; ?>  </td>
<td><?= $row['historico']; ?>  </td>

0

You can put the collpase div in the first while so:

<div class="row" id="realTimeContents myGroup">
    <div class="col-md-8">
        <div class="scrollable">
            <table class="table table-striped table-dark text-center">
                <thead>
                    <tr>
                        <td>ID</td>
                        <td>Nome</td>
                        <td>CPF/CNPJ</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $sql = "SELECT * FROM registro";
                        $result = mysqli_query($conexao, $sql);
                        $queryResults = mysqli_num_rows ($result);

                        if ($queryResults > 0) {
                        while ($row = mysqli_fetch_assoc($result)) {
                            echo "
                                <tr>
                                    <td scope='row'>".$row['id']."</td>
                                    <td><a class='' data-toggle='collapse' aria-expanded='false' aria-controls='teste' href='#teste_". $row['id'] ."'</a></td>
                                    <td>".$row['CPF']."</td>
                                </tr>
                              <div class='collapse' id='teste_". $row['id'] ."'>
                                    <td>".$row['nome']."</td>
                                    <td>".$row['CPF']."</td>
                                    <td>".$row['data_de_nascimento']."</td>
                                    <td>".$row['numero_de_cadastro']."</td>
                                    <td>".$row['CEP']."</td>
                                    <td>".$row['numero']."</td>
                                    <td>".$row['bairro']."</td>
                                    <td>".$row['endereço']."</td>
                                    <td>".$row['cidade']."</td>
                                    <td>".$row['complemento']."</td>
                                    <td>".$row['uf']."</td>
                                    <td>".$row['telefone_residencial']."</td>
                                    <td>".$row['telefone_comercial']."</td>
                                    <td>".$row['categoria_de_cliente']."</td>
                                    <td>".$row['telefone_celular']."</td>
                                    <td>".$row['telefone_0800']."</td>
                                    <td>".$row['insc_estadual']."</td>
                                    <td>".$row['CNPJ']."</td>
                                    <td>".$row['RG']."</td>
                                    <td>".$row['insc_municipal']."</td>
                                    <td>".$row['PIS']."</td>
                                    <td>".$row['site']."</td>
                                    <td>".$row['email']."</td>
                                    <td>".$row['historico']."</td>
                              </div>


                            ";
                            }
                        }
                    ?>
                </tbody>

            </table>
        </div>
    </div>
</div>

Simply place an identifacador in the id of the div Collapse to link all div generated in the database query

  • Here the Collapse function is not working, the data gets "locked" on the screen and so the hide button does not work.

Browser other questions tagged

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