0
I’m making a cart in php. What I want is to get the name of the product (which is in the database) from its id, which is the value of each position of the array. However, since there are multiple "products" in the cart vector, how do I create a loop to search the product in the database from each vector position?
Vector:
$_SESSION['carrinho'][] = $_GET['produtoid'];
BD search code from the id that is stored at each vector position:
for($i = 0; $i < count($_SESSION['carrinho']); $i++){
$consulta = "SELECT * FROM produto where id = $_SESSION['carrinho'][$i] ";
$resultado = $PDO->query($consulta);
$vetorResultado = $resultado->fetchAll(PDO::FETCH_ASSOC);
foreach ($vetorResultado as $valor) {
echo $valor['nome'];
}
}
But when I execute the following error appears:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\projeto\carrinho.php on line 24
The error must be in the query, add keys around the Session. Like this: "SELECT * FROM product WHERE id = {$_SESSION['cart'][$i]} ";
– lvr7