A doubt of PHP

Asked

Viewed 36 times

1

Hello, I have a question on how to make an entry in a database.

while($row = $result->fetch_assoc()) {

        echo "<div class=\"card\" style=\"width: 18rem;\">";
            //echo "<img class=\"card-img-top\" src=\"...\" alt=\"Card image cap\">";
        echo '<img class=\"card-img-top\" src="data:image/jpeg;base64,' . base64_encode($row['imagem']) .'"/>';
        echo "<div class=\"card-body\">";
        echo "<h5 class=\"card-title\">Card title</h5>";
        echo "<p class=\"card-text\">Some quick example text to build on the card title and make up the bulk of the card's content.</p>";
        echo "</div>";
        echo "<ul class=\"list-group list-group-flush\">";
        echo "<li name=\"prod\"class=\"list-group-item\">".$row['id_Produto']."</li>";
        echo "<li class=\"list-group-item\">".$row['nome']."</li>";
        echo "<li class=\"list-group-item\">Vestibulum at eros</li>";
        echo "</ul>";
        echo "<div class=\"card-body\">";
        echo "<a href = \"adicionarCarrinho.php\" method = \"post\"class=\"card-link\">Adicionar ao carrinho</a>";
        echo "</div>";
        echo "</div>";

What I’m trying to do is when the user clicks the button echo "<a href = \"adicionarCarrinho.php\" method = \"post\"class=\"card-link\">Adicionar ao carrinho</a>"; a php file get the .$row['id_Produto']. and add in an ordered table where it contains id_order and a foreign key producto_id. In case they would have multiple products and one user could add several to the order.

This can only be done in php(college work) Thank you

  • I believe the best way is using ajax.

  • I can’t, PHP only required

  • then create a form with only the subtmit button on each product card. and every time you submit add to cart and redirect to that same page.

  • and an Hidden type with the product id.

1 answer

1


I made a little example of how I would do, maybe not the right answer to your case: - I made an array $row, simulating the data coming from the bank, but that can be replaced and appropriate to each case.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
    <?php
    $row = [['id_produto' => 1], ['id_produto' => 2], ['id_produto' => 3], ['id_produto' => 3], ['id_produto' => 3], ['id_produto' => 3], ['id_produto' => 3], ['id_produto' => 3], ['id_produto' => 3], ['id_produto' => 3]];
    $qtdProdutos = 10;
    for ($i=0; $i <= ($qtdProdutos-1); $i++):?>
        <div class="card">
            <form action="adicionarcarrinho.php" method="POST">
                <input type="hidden" name="id_produto" value="<?= $row[$i]['id_produto']?>">
                <input type="submit" value="Adicionar ao Carrinho">
            </form>
        </div>
    <?php endfor; ?>
</body>
</html>
  • Helped me guy, using "value" I managed to at least print the value on another page

  • this, now just register the product id in the order table, a suggestion is if you have users, register also the user id. there you take 10

  • This I’m not getting, is not giving error but does not insert in the bank

  • discovered missing $stmt->execute();

  • 1

    Thanks for helping

Browser other questions tagged

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