You can reference the entire session array with just $_SESSION. Also, you have specified:
as $key => $value
but in your insert statement you reference $qty.
Try:
foreach ($_SESSION as $key => $value) {
$test = " INSERT INTO cartcontents (cartcontents_id, product_id, quantity)
values ('" . $_SESSION['cart']. "', '" . $key . "' , '" . $qty . "')";
} //end of foereach loop
What value are you hoping to pull with $_SESSION['cart']? This references a session variable, not the entire session.