1
My problem is this, I hope you can help me:
I am searching some records from the "Products" table of my database and displaying it on the front. I am using a Foreach to bring these records and there is also a button that will create a session and add these products to my cart. The problem is not in the cart itself but in the list where is located the button that will add. It calls an Action referencing whether there is an action (the action, right) and the product id. The path looks something like this:
---> localhost/Loginandregisterlanchenet/carrinho.php? add=cart&id=
The id is empty and does not refer to anything resulting in a URL error not found. Below is the code that below:
<?php
$conect = new db();
$conect = $conect ->conectar();
$query = mysqli_query($conect, "SELECT * FROM produto");
$result = mysqli_fetch_all($query);
echo "<div class='quadcard1'>CRIAR RESERVA<br><br>";
echo "<div class='quadcard2' > Cardápio<br><br>";
foreach ($result as $item){
echo "<div class='quadcard'>";
echo "<div class='produtos'>".$item[1]."<a href='carrinho.php?add=carrinho&id='".$item[0]."'><input class='btn btn-primary' type='button' value='+' style='display: relative; float : right; margin-right :1rem; margin-top: 0.5rem;'></a><br>".$item[3]." <label class='valor'> Valor: R$ ".$item [5]."</label></div></div>";
}
echo "<div></div>";
echo "</div></div>";
?>
Someone was able to identify the mistake?
In case you need more details, just say that I add.
Grateful! Att.
Before foreach I recommend you check what’s inside your result. Try adding this before foreach: print_r($result)
– Fernando Nunes
It should not give URL error not found because of id, since it is only a URL parameter. The page itself is
carrinho.php
.– Sam
I think that
$item[0]
is not the value you are waiting for. As suggested above, aprint_r($result)
before the foreach, and evenprint_r($item)
within the cycle can help you understand how the array is structured.– Leite