Member Avatar for jpknoob

Hello, i attempting to edit an online payment form to connect to MySQl, i have managed to get everyting working up until the payment page. The problem i am having is sending the cart contents via encrytion to the server. When there is one product, there are no issues. If there is more than one product in the cart, only the last product is sent through. My code to connect to the database is;

foreach($_SESSION['cart'] as $product_id => $quantity) {
$query = sprintf("SELECT * FROM all_products WHERE part_number = '%s';",$product_id);  
									
$result = mysql_query($query)or die(mysql_error());
$num = mysql_num_rows($result); 
									
if(mysql_num_rows($result) > 0) {
  list($id, $part_number, $price) = mysql_fetch_row($result);
								
  $line_cost = $price * $quantity;
  $total = $total + $line_cost;	
								
}}

The template that i am using builds the cart like so;

$sngTotal=0.0;
$strThisEntry=$strCart;
$strBasket="";
$iBasketItems=0;
							
while (strlen($strThisEntry)>0) {
	// Add another item to our Form basket
	$iBasketItems=$iBasketItems+1;
		
	$total=$total + $quantity * $product_id;
	$strBasket=$strBasket . ":" . $product_id . ":" . $quantity;
	$strBasket=$strBasket . ":" . number_format($price,2); /** Item price **/
	$strBasket=$strBasket . ":" . number_format($price*$quantity,2); /** Line total **/			
						
	// Move to the next cart entry, if there is one
	$pos=strpos($strThisEntry,",");
	if ($pos==0) 
		$strThisEntry="";
	else
		$strThisEntry=substr($strThisEntry,strpos($strThisEntry,",")+1);
}

The code from the template has got me completely confused and any help to fix this would be greatly appreciated.

I have merged template in your code, I think following 3 variable are important for your payment gateway. Do not include template code now. (its already in your code now)

$sngTotal=0.0;
$strBasket="";
$iBasketItems=0;

Final code

<?php 		

$sngTotal=0.0;
$strBasket="";
$iBasketItems=0;
$total=0;

foreach($_SESSION['cart'] as $product_id => $quantity) 
{
	$query = sprintf("SELECT * FROM all_products WHERE part_number = '%s';",$product_id);  
									
	$result = mysql_query($query)or die(mysql_error());
	$num = mysql_num_rows($result); 
									
	if(mysql_num_rows($result) > 0) 
	{
	  list($id, $part_number, $price) = mysql_fetch_row($result);
	
	  $iBasketItems=$iBasketItems+1;							
	  $line_cost = $price * $quantity;
	  $total = $total + $line_cost;	

	  $strBasket=$strBasket . ":" . $product_id . ":" . $quantity;
   	  $strBasket=$strBasket . ":" . number_format($price,2); /** Item price **/
	  $strBasket=$strBasket . ":" . number_format($price*$quantity,2); /** Line total **/
								
	}
}
$sngTotal=$total;
?>
Member Avatar for jpknoob

Thank you so much for your reply and it has indeed fixed my problem. My tests work without any errors now!

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.