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

Asked

Viewed 47 times

0

So guys, I’m starting now to learn how to mess with php and I’m trying to make a registration system whose will be registering in it activity. I’ve already got a lot of stuff, I was able to connect the bank, I was able to save data in phpmyadmin through a form. However, when I want to show the user the data recorded 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 grateful ;-; `

    <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>

`

  • where are you doing the select in the database? you need this before calling the ->fetchall method, and where you are declaring the $stmt variable?

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 expect with an assignment to $user within while, the function fetchall should return only one result at a time (one row of your database table), and when it ends up returning a false.

If it returns all as the name seems to say, you should assign her result to a variable and iterate over it later, like 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 some bank access library or you who created that class that you called the $stmt->fetchall(PDO::FETCH_ASSOC)?

Browser other questions tagged

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