im making a small web app as e-commerce site

im storing the product id selected by a visitor into session product id as an array

now what i want to accomplish is:

also store the quantity associated with the product selected

and then update that sessions for quantity if visitor updates the quantity field
of a certain product
lets say in a cart view page

thanks!

Dani AI

Generated

Good progress — ’s update approach and ’s multidimensional solution get the job done. The notes below cover practical follow-ups and common pitfalls for anyone implementing a session-based cart later on.

Keep the session payload small and authoritative. Store only a product identifier and the quantity in the session, and always read product details (name, price, stock) from the database when rendering the cart. Never trust client-submitted prices. Recalculate totals on the server each time so a stale session cannot produce incorrect charges.

Validate and bound quantities server-side. Coerce posted quantities to integers, reject negatives, clamp upper limits to stock or a sane per-order max, and treat zero as removal. Protect update endpoints with CSRF tokens and validate inputs with PHP filters rather than relying on client-side checks.

Manage sessions and security. Call session_start() before reading/writing session data and set cookie flags (HttpOnly, Secure, SameSite) where appropriate. Regenerate the session id on login/checkout to reduce fixation risk. For reference on PHP session handling, see PHP sessions.

Plan for persistence and UX edge cases. If users can log in, persist and merge the session cart into a database so carts survive devices and browsers; define a clear merge policy (sum quantities or prefer one source). Handle multi-tab updates and concurrent edits by applying server-side checks and returning up-to-date counts after each change. Always verify available stock before confirming an order and surface helpful messages when requested quantities cannot be met.

Recommended Answers

All 2 Replies

This might help you work out what you need to do

if (!is_array($_SESSION['basket']))
{
	$_SESSION['basket']=array();
}

if (is_array($_POST['qty'])) {
	foreach ($_POST['qty'] as $key=>$quantity) {
		$quantity=ceil($quantity);
		if ($quantity==0) {
			unset($_SESSION['basket'][$key]);
		} else {
			$_SESSION['basket'][$key]['qty']=$quantity;
		}
	}
}

i figured it out. thanks! i use multi dimensional array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.