Help in displaying data saved in a database on a php page

0

So, guys, I'm just starting to learn how to mess with php and I'm trying to make a registration system that will register on it. I already managed a lot, I could connect the bank, I was able to save data in phpmyadmin through a form. However, when I want to show the user the data registered in a table on a web page, it does not show even the data being saved in the database. Can someone help me? I'll be thankful ;-; '              

    <title>Sistema de Registro de Atividades</title>
</head>

<body>

    <h1>Sistema de Registro de Atividades</h1>

    <p><a href="form-add.php">Adicionar atividade</a></p>

    <h2>Lista de atividades</h2>

    <p>Total de de atividades: <?php echo $total ?></p>

    <?php if ($total > 0): ?>

    <table width="50%" border="1">
        <thead>
            <tr>
                <th>Nome da Atividade</th>
                <th>duracao</th>
                <th>descricao</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($user = $stmt->fetchall(PDO::FETCH_ASSOC)): ?>
            <tr>
                <td><?php echo $user['atividade_nome'] ?></td>
                <td><?php echo $user['duracao'] ?></td>
                <td><?php echo $user['descricao'] ?></td>
                <td>
                    <a href="form-edit.php?id=<?php echo $user['id'] ?>">Editar</a>
                    <a href="delete.php?id=<?php echo $user['id'] ?>" onclick="return confirm('Tem certeza de que deseja remover esta atividade?');">Remover</a>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>

    <?php else: ?>

    <p>Nenhum usuário registrado</p>

    <?php endif; ?>
</body>

'

    
asked by anonymous 09.10.2018 / 18:56

1 answer

1

This function in $user = $stmt->fetchall(PDO::FETCH_ASSOC) by name seems to return an associative array with all results, for this function to function as you wait with an assignment to $ user within the while, the function fetchall should return only one result at a time (one line from your database table), and when it finishes returning a false.

If it returns all as the name seems to say, you should assign the result to a variable and iterate over it afterwards, type this:

<?php $users = $stmt->fetchall(PDO::FETCH_ASSOC); ?>
<?php foreach($users as $user): ?>
    <tr>
        <td><?php echo $user['atividade_nome'] ?></td>
        <td><?php echo $user['duracao'] ?></td>
        <td><?php echo $user['descricao'] ?></td>
        <td>
            <a href="form-edit.php?id=<?php echo $user['id'] ?>">Editar</a>
            <a href="delete.php?id=<?php echo $user['id'] ?>" onclick="return confirm('Tem certeza de que deseja remover esta atividade?');">Remover</a>
        </td>
    </tr>
<?php endwhile; ?>

Are you using a bank access library or did you create this class that you called $stmt->fetchall(PDO::FETCH_ASSOC) ?

    
09.10.2018 / 19:17