If you store the values of the cart in an a session variable, lets say $_SESSION['cart'] to save the data from the session cart to the database you would normally do something like
$data = $_SESSION['cart'];
if(empty($data)) return false;
$table = 'purchase';
$implode_values = array();
$implode_fields = array();
foreach($data as $key => $value) {
$implode_fields[] = (string)$key;
$implode_values[] = Your_db_escape_method($value);
}
if( mysql_query ("INSERT INTO " . $table . " (".implode(", ", $implode_fields).") VALUES (".implode(", ",$implode_values).")")) {
//echo 'Data was inserted';
} else {
//echo 'Data was not inserted';
}
or as single query as per your example
foreach($data as $value) {
mysql_query (
"INSERT INTO
purchase(
username,
gameid,
amount_purchased
) VALUES (
'".$value['username']."',
'".$value['gameid']."',
'".$value['amount_purchased']."')"
);
}
Set the values from your object and store them like this;
$_SESSION['cart'] = array (
'username' => 'Usertest_1', // eg 'username' => $obj->username,
'gameid' => 1234,
'amount_purchased' => 200
);
The above is the same as;
$_SESSION['cart']['username'] = 'Usertest_1';
$_SESSION['cart']['gameid'] = 1234;
$_SESSION['cart']['amount_purchased'] = 200;
// for multiple records;
$_SESSION['cart'][] = array (
'username' => 'Username1',
'gameid' => 1234,
'amount_purchased' => 200
);
$_SESSION['cart'][] = array (
'username' => 'username2',
'gameid' => 1234,
'amount_purchased' => 200
);
save example
$data = $_SESSION['cart'];
$result = mysql_query ("INSERT INTO purchase(username, gameid, amount_purchased) VALUES ('".$data['username']."','".$data['gameid']."','".$data['amount_purchased']."')");
if($result && mysql_num_rows($result)>0) {
// all ok
}