1
I’m doing a shopping cart simulation with PHP and Mysql.
I need to store a session_id
or any user session identification, for when it leaves I remove the data that it stored in the database.
I already created a column in the database to store this session_id
.
Can someone help me with this?
I’m trying to use $_SESSION to store the products:
$dadosProdutos = filter_input_array(INPUT_POST, FILTER_DEFAULT);
if ( isset($dadosProdutos) ):
unset( $dadosProdutos['ProdutoCesta'] );
$_SESSION['cesta'] = $dadosProdutos;
endif;
If the Submit button is clicked it takes the data via post and stores it in the session. It’s working, but when I go to insert a new product it cleans the session.
Assuming you store the session_id, what will it serve you when the user leaves? And how will you know it’s out? It would not be easier to save the cart to Session (and pass to DB only when the user logs in or registers)?
– Bacco
I thought that too, Bacco! But I know little about handling Session. Can you give me a hand? do I use $_SESSION? and after the data is stored in Session how do I delete the products by ID for example?
– Carlos Henrique
If you store the products in an array within Session, the array will be deleted by PHP itself after Session expires. Give a trained on the basics of Session and a researched right here on the site, about "Session" and "PHP" in the search up there to see examples. It’s gonna be easier than starting straight with the cart. I think it’s nice to record the cart in Mysql, because if the user loses the connection, or restart the PC does not lose the cart, but for this it has to be logged in to your system. Then you will delete after a number of days the cart is not accessed. No.
– Bacco
Starting point: http://answall.com/search?q=session+php+is%3Aquestion
– Bacco
I will search yes, Bacco! Give me only one direction?! Do I create a Session for the cart? And I have to loop the cart page to display each product?
– Carlos Henrique
The Session serves to store a lot of information from the same use session (in principle, it is worth the time the customer interacts with the site). The cart will be one of those things. You can do something that generates a structure like
$_SESSION['carrinho'] = array( array( 'idProduto' => 19, 'qtdProduto' => 2 ), array( 'idProduto' => 7, 'qtdProduto => 1 ), array( ...
. The rest (product name, price etc.) you take from the DB when displaying (or caching in memory or session). I’m just sketching out the idea, then you have to elaborate better.– Bacco
I was able to store the data in Session. Only I am sending the data from a form. Every time I put it in the basket it clears the session. $dadosProdutos = filter_input_array(INPUT_POST, FILTER_DEFAULT);
 if ( isset($dadosProdutos) ):
 unset( $dadosProdutos['ProdutoCesta'] );
 $_SESSION['cesta'] = $dadosProdutos; @Bacco
– Carlos Henrique
This is why you are overwriting the basket, not adding. Note that in my example is an array of products, with each product in another array. Dunga Cardoso’s solution just below is correct.
– Bacco