1
Hello! I will try to explain my purpose in the best possible way.
I started studying PHP a little while ago, I am doing a registration system and buying products only for learning, but I am facing a small problem.
I have my products registered with their proper keys in a datatable. I manipulate these products, logically, through a connection to the database. Next I have the page php products. which is intended to display the products present in the database and allow the logged-in user to add them to their shopping cart. The problem is: I’m not getting the quantity inserted in input for the requested product. Follow my file php products.:
$sql = "SELECT id_produto, nome_produto, preco_produto FROM produtos ORDER BY id_produto";
$stmt = $conexao->prepare($sql);
$stmt->execute();
$num = $stmt->rowCount();
if($num>0)
{
echo "<table>";
echo " <tr>";
echo " <th>NOME</th>";
echo " <th>PREÇO</th>";
echo " <th>QUANTIDADE</th>";
echo " </tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$preco_produto_reajustado = number_format($preco_produto, 2, ",", ".");
echo " <tr>";
echo " <td><div class='id-produto' style='display: none'>{$id_produto}</div>";
echo " <div class='nome-produto'>{$nome_produto}</div></td>";
echo " <td>R${$preco_produto_reajustado}</td>";
echo " <td>";
echo " <form class='adicionar'>";
echo " <input type='number' name='quantidade' value='1' min='1' max='20'/>";
echo " <button type='submit'>Adicionar</button>";
echo " </form>";
echo " </td>";
echo " </tr>";
}
echo "</table>";
}
else
{
echo "Sem produtos.";
}
As you can see, the add link points to the file add.php. In this file I create a SESSION and store the matrices I obtained from the Row previously selected in php products.. This way, the product in question is stored in SESSION along with its details. Follow my file add.php:
<?php
require_once("detectar-erros.php");
session_start();
$id_produto = isset($_GET['id_produto']) ? $_GET['id_produto'] : "";
$nome_produto = isset($_GET['nome_produto']) ? $_GET['nome_produto'] : "";
$quantidade = isset($_GET['quantidade']) ? $_GET['quantidade'] : "";
if(!isset($_SESSION['carrinho']))
{
$_SESSION['carrinho'] = array();
}
if(array_key_exists($id_produto, $_SESSION['carrinho'])){
header('Location: produtos.php?acao=existe&id_produto' . $id_produto . '&nome_produto=' . $nome_produto);
}
else
{
$_SESSION['carrinho'][$id_produto]=$nome_produto;
header('Location: produtos.php?acao=adicionado&id_produto' . $id_produto . '&nome_produto=' . $nome_produto);
}
?>
I’m not sure how to set the value of input for the variable $quantidade
.
How to proceed? I thank you in advance for any help and ask for forgiveness for any mistakes!
I think the best way was to use a POST form and forget about GET. Or else you have to use JS to change the value.
– Jorge B.
@Jorgeb.I’ve been trying something similar, but it seems that what makes it impossible for me to do this is the while loop, because it’s creating several inputs with the same name, and the way I put it here in the code is not working
<input type='number' name="quantidade[$row['id']]" value='1'/>
– M. Victor
you can’t give this
name
because you can have several equal amounts. You have to give onename
that doesn’t happen again so you can catch theinput
afterward– Jorge B.
@Jorgeb. After much study I was able to solve my problem by myself using jQuery and matrices for the SESSION. Thanks anyway.
– M. Victor
Make an answer with that.
– Jorge B.
@Jorgeb. Done.
– M. Victor