I have a PHP code, where I can find productId in first table
$stmt = $db->prepare("SELECT productId FROM boughtProducts WHERE userid = :username");
$stmt->execute(array(':username' => $_SESSION['username']));
$productId = $stmt->fetchAll();
$productId["0"]["productId"] & $productId["1"]["productId"]...
$stmt = $db->prepare("SELECT price FROM products WHERE id = :productid");
$stmt->execute(array(':productid' => $productId["0"]["productId"]));
$price = $stmt->fetch(PDO::FETCH_ASSOC);
Something along these lines might work:
SELECT SUM(products.price) FROM boughtProducts, products WHERE boughtProducts.userid = :username and products.id = boughtProducts.productId
Read something about the
JOIN keyword (I'm using an implicit join in my example)... also get yourself acquainted with the concept of foreign keys.