Jump to content

Save Session Data To Database


son
 Share

Recommended Posts

I have a cart that works well as:

if (isset($_REQUEST['do']) && ($_REQUEST['do'] == 'add')){if (isset($_GET['qty2']))	{	$qty2 = (int) $_GET['qty2'];	}	else	{	$qty2 = 1;	}if (isset($_GET['finish']))	{	$finish = (int) $_GET['finish'];	}	else	{	$finish = FALSE;	}		if (isset($_GET['id']))	{	$id = (int) $_GET['id'];		if ($id > 0)		{		$id_query = "SELECT product, price FROM products WHERE product_id = $id";		$id_result = mysqli_query ($dbc, $id_query);			if (mysqli_num_rows($id_result) == 1)			{			list ($product, $price) = mysqli_fetch_array($id_result, MYSQLI_NUM);				if (isset($_SESSION['cart'][$id]))				{				$_SESSION['cart'][$id] += $qty2;;				echo "<p>Another copy of the following item has been added to your cart: <strong>$product</strong>.</p>\n";				}				else				{				$_SESSION['cart'][$id] = $qty2;				echo "<p>The following item has been added to your cart: <strong>$product</strong>.</p>\n";				}			}		}	}}elseif (isset($_REQUEST['do']) && ($_REQUEST['do'] == 'update')){	foreach ($_POST['qty'] as $k => $v)	{	$pid = (int) $k;	$qty = (int) $v;		if ($qty == 0)		{		unset ($_SESSION['cart'][$pid]);		}		elseif ($qty > 0)		{		$_SESSION['cart'][$pid] = $qty;		}	}	echo "<p>The add to cart has been updated.</p>";}if ($_SESSION['cart'] && !empty($_SESSION['cart'])){$cart_query = "SELECT product_id, product, code, detail, price, discount_id, img1 FROM products WHERE product_id IN (";	foreach($_SESSION['cart'] as $id => $v)	{	$cart_query .= (int) $id . ',';	}$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY product";$cart_result = mysqli_query ($dbc, $cart_query);	if (mysqli_num_rows($cart_result) > 0)	{	echo "<table style=\"margin-top:15px;\">";	echo "<tr><th>Item</th><th>Description</th><th>Qty</th><th class=\"right\">Price</th></tr>";	echo "<form action=\"cart.php\" method=\"post\">";	echo "<input type=\"hidden\" name=\"do\" value=\"update\" />";	$total = 0;		while ($row = mysqli_fetch_array($cart_result, MYSQLI_ASSOC))		{		$product_name = $row['product'];		$product_code = $row['code'];		$product_detail = $row['detail'];		$price = $row['price'];		$price2 = $row['price'];		$price2 = number_format($price2, 2);		if ($row['discount_id'] != '')		{		$discount = $row['discount_id'];		$discount = (int) $discount;		}		else		{		$discount = 0;		}		$img = $row['img1'];		$size = getimagesize("products/basket/{$img}");	$subtotal2 = $_SESSION['cart'][$row['product_id']]* $price;	$total2 += $subtotal2;	$delivery = 0;	$grand_total = $total2 + $delivery;	$price = number_format($price, 2);			echo "<tr><td><img src=\"" . $basketPath . $img . "\" " . $size[3] . " alt=\"" . $product_name . "\" /></td>";		echo "<td>" . $product_name . "<br />Product Code: " . $product_code . "<br />Size in cm: " . $product_detail . "<br  />";		$q2 = 'SELECT * FROM ageing ORDER BY ageing_id';		$r2 = mysqli_query($dbc, $q2);			if (mysqli_num_rows($r2) > 0)			{			?>			<label for="ageing">Ageing:</label> <select name="ageing" id="ageing">			<?php				while (list($ageing_id, $ageing_name) = mysqli_fetch_array($r2, MYSQLI_NUM)) 				{				echo "<option value=\"$ageing_id\"";					if((isset($_POST['ageing'])) && ($_POST['ageing'] == $ageing_id))					{ 					echo ' selected="selected" '; 					} 					echo ">" . $ageing_name . "</option>\n";				}			echo "</select><br  />";		}		if ($finish)		{		// check for finishes		$finish_query2 = "SELECT finish_id, finish FROM finishes WHERE finish_id  = $finish";		$finish_result2 = mysqli_query ($dbc, $finish_query2);			if (mysqli_num_rows($finish_result2) > 0)			{			echo "Finish: ";			$row=mysqli_fetch_assoc($catNameR); 				$finish2_id = $row['finish_id'];				$finish2 = $row['finish'];				echo $finish2;				echo "<br />";			}		}		echo "</td>";		echo "<td><input type=\"text\" style=\"width: 2em\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]}\" />";		if ($total >=500 && ($discount == 1 || $discount == 2))		{		echo "<p style=\"margin-left:0;\"><span class=\"small\">Per item: £" . $price . "</p>";		}		elseif ($total <=500 || $discount == 0)		{		echo "<p style=\"margin-left:0;\"><span class=\"small\">Per item: £" . $price . "</p>";		}		echo "</td>";		echo "<td class=\"right\">£" . number_format($subtotal2, 2) . "</td></tr>\n";		}		echo "<tr><td colspan=\"3\" class=\"right noBorder\">Sub Total:</td><td class=\"right\">£ " . number_format($total2, 2) . "</td></tr>";		echo "<tr><td colspan=\"3\" class=\"right noBorder\">Delivery:</td><td class=\"right\">£ " . number_format($delivery, 2) . "</td></tr>";		echo "<tr><td colspan=\"3\" class=\"right noBorder\"><strong>Grand Total:</strong></td><td class=\"right\"><strong>£ " . number_format($grand_total, 2) . "</strong></td></tr>";		echo "<tr><td colspan=\"4\" class=\"noBorder\"> </td></tr>";		echo "<tr><td colspan=\"4\" class=\"right noBorder\" style=\"padding-right:0;\">";		if (!isset($_SESSION['first_name'])) 		{		echo "<a href=\"login.php\" title=\"Login first to save data\">";		}		else		{		echo "<a href=\"save.php\" title=\"Save data\">";		}		echo "<INPUT type=\"submit\" value=\"Save data\" id=\"save\" name=\"save\" ></a> ";		if (isset($_SESSION['back']) && $_SESSION['back'] != '')		{		$backlink = $_SESSION['back'];		}		else		{		$backlink = 'index.php';		}		echo "<a href=\"" . $backlink . "\" title=\"Continue Shopping\"><INPUT type=\"submit\" value=\"Continue Shopping\" id=\"continue\" name=\"continue\"></a> ";		echo "<INPUT type=\"submit\" value=\"Update Total\" id=\"submit\" name=\"submit\">";		echo "</td></tr>";		echo "</table>";		echo "</form>";	}}else{echo "<p>Currently there are no items added to this page.</p>";}

Now, the new feature I am after is that when if (isset($_SESSION['first_name'])) I would like to have save button save product id, quantity, finish id and and ageing along with user id to database (should be used to display at later stage)? How do I get data from all rows inserted into db?Son

Link to comment
Share on other sites

Which part are you having trouble with? You just need to set up one or more tables to hold the data you want, and then insert it.
I have got one table to hold all the mentioned data as int:product id , quantity, finish id, ageing, user id (is reference to other table that hold more details about user).isset($_REQUEST['do']) && ($_REQUEST['do'] == 'update') checks for changes of quantity, but now I am not sure what is best Way to check that 'Save data' was pressed and if yes, how to get all data individually to be inserted into table rows. I never inserted more than one row at a time before.Son
Link to comment
Share on other sites

but now I am not sure what is best Way to check that 'Save data' was pressed
You can either give the submit button a name and check for that name in $_POST, or use a hidden form element to tell the PHP script what to do. There are examples of both of those here:http://w3schools.invisionzone.com/index.php?showtopic=12509
I never inserted more than one row at a time before.
An insert query is an insert query, if you have an insert query inside a loop it's no different than an insert query not in a loop.
Link to comment
Share on other sites

You can either give the submit button a name and check for that name in $_POST, or use a hidden form element to tell the PHP script what to do. There are examples of both of those here:http://w3schools.invisionzone.com/index.php?showtopic=12509An insert query is an insert query, if you have an insert query inside a loop it's no different than an insert query not in a loop.
Cheers,I will go for it tomorrow and see if that brings me closer to what I am after...Son
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...