session_id PHP and mysql

Asked

Viewed 365 times

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)?

  • 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?

  • 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.

  • Starting point: http://answall.com/search?q=session+php+is%3Aquestion

  • 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?

  • 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.

  • 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

  • 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.

Show 3 more comments

1 answer

0


You better use array in SESSION, as for your script

 $_SESSION['cesta'][]
  • 2

    Dunga, the path you proposed is a good alternative, but it would be nice for you to explain how your answer works, so you have a better chance of it being well punctuated.

  • Guys, I was able to store the product Ids on Session and get the products from the selected database Ids. But as I have got up to three prices on the same product, it got kind of winded doing this with Session. I found it simpler to put the orders in another table in the bank with a column for session and it worked. I will delete the database data after a day or so. I appreciate your interest in helping! Thank you very much!

  • Dunga, can you explain the answer better? so others can better understand the problem and the solution

Browser other questions tagged

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