title: Updating products stocks in database
body: <p>I want to update the stock quantities of my products in the DB after a purchase.</p>
  
  <p>My code already works fine, but I want to know if there is a best way to do, with only one SQL statement?</p>
  
  <pre><code>// All the product in the member's cart
  if ($stmt = $conn-&gt;prepare("SELECT product_id, quantity FROM tbl_cart WHERE member_id = ?")) {
    $stmt-&gt;bind_param("i", $memberId);
    $stmt-&gt;execute();
    $result = $stmt-&gt;get_result();
    $stmt-&gt;close();
    $cartItem = $result-&gt;fetch_all(MYSQLI_ASSOC);
  
  // Set the quantity after purchase
    foreach ($cartItem as $key =&gt; $item) {
      $stmt = $conn-&gt;prepare("UPDATE tbl_product SET stock = stock-? WHERE id = ?");
      $stmt-&gt;bind_param("ii", $item['quantity'], $item['product_id']);
      $stmt-&gt;execute();
      $stmt-&gt;close();
    }
  }
  </code></pre>