Take Database info from the array element

Asked

Viewed 103 times

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]} ";

1 answer

1

I don’t know if it’s a horrible mess but it’s what I think might work, $query = "SELECT * FROM product Where id = $_SESSION['cart'][$i] "; that change for

$cart = $_SESSION['cart'][$i]; $query = "SELECT * FROM product Where id = '$affection'" ;

or

$query = "SELECT * FROM product Where id = '$_SESSION['cart'][$i]' ";

this so php knows that there is something in php and not just a string.

Browser other questions tagged

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