Database variables are not added in $_SESSION array

Asked

Viewed 83 times

2

I am building a cart for an application. I want to add the name, quantity and price of the product inside the cart. For this, I do:

In the header I include in the pages accessed by the client:

$_SESSION['cart']=isset($_SESSION['cart']) ? $_SESSION['cart'] : array();

On the page where the customer makes the order:

$query_product = "SELECT * FROM PRODUCTS WHERE ID = '$id'";
$result = mysqli_query($db, $query_product);
$product_info = mysqli_fetch_assoc($result);
$name = $product_info['name'];
$price = $product_info['price'];
echo $name, $price;
///a variável amount é pega dentro de um input html

Echo output is something like:

Bombom de côco3.00

When I will add the variables I took from the database within $_SESSION['cart'], they never appear. I have tried to assign values with and without the array_push(), as follows:

function addCart(){
    global $amount, $name, $price;
    $_SESSION['cart'][] = $name;
    $_SESSION['cart'][] = $amount;
    $_SESSION['cart'][] = $price;
    // array_push($_SESSION['cart'], $product_info['name']);
    // array_push($_SESSION['cart'], $amount);
    // array_push($_SESSION['cart'], $price);
    print_r($_SESSION['cart']);
}

The output of this is:

Array ( [0] => [1] => 5 [2] => )

It only takes the informed amount in the input. Why can’t I add the values I took from the database?

Function call:

if(isset($_POST['btn-register'])){
    addCart();
    Print '<script>window.location="cart.php";</script>';
}

EDIT: I tried to repeat the query inside the function addCart() (I ended up removing the function and putting its content inside the if), but I discovered that the $id was empty:

if(isset($_POST['btn-register'])){
    $id=$_GET[id];
    $db = mysqli_connect(/*parâmetros da conexão*/) or die(mysqli_error());
    mysqli_query($db, "SET NAMES 'utf8'");
    mysqli_query($db, 'SET character_set_connection=utf8');
    mysqli_query($db, 'SET character_set_client=utf8');
    mysqli_query($db, 'SET character_set_results=utf8');
    $query_product = "SELECT * FROM products WHERE ID = '$id'";
    echo $query_product;
    $result = mysqli_query($db, $query_product);
    $product_info = mysqli_fetch_assoc($result);
    $name = $product_info['name'];
    $price = $product_info['price'];
    $_SESSION['cart'][] = $name;
    $_SESSION['cart'][] = $amount;
    $_SESSION['cart'][] = $price;
    print_r($_SESSION['cart']);
}

I give the get after clicking a button on a form. Does it influence something? The action form is the own page.

EDIT: I was able to solve it, I just don’t know if it’s the best way: I was trying to get the $id after tightening the submit of the form, then I changed the action of the form action="pagina.php" for action="pagina.php?id=<?php echo $id;?>". I couldn’t do it within the function addCart(), then really had to put the content of the function inside the if where I called her.

  • 1

    You already gave echo within the function addCart() before trying to insert into $_SESSION?

  • @Lucas, I just tried, no value is displayed... I tried to pass the values by parameter, but it also didn’t work

  • So the error is at the time of calling this function, and not within the function, you can post the code where you call this function?

  • @Lucas just posted!

  • the variable $_POST['btn-register'] receives the $id of $query_product ?

  • @Lucas no, this variable is the name of an html input that works as form Ubmit

Show 1 more comment
No answers

Browser other questions tagged

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