Add-cart.php: Num

$stmt = $conn->prepare("SELECT price, stock FROM products WHERE id = ? AND active = 1"); $stmt->bind_param("i", $product_id); $stmt->execute(); Principle 4: Implement CSRF Tokens Since you are modifying state (the cart), every request must include a unique token.

// Vulnerable code $id = $_GET['num']; $result = mysqli_query($conn, "SELECT * FROM products WHERE id = $id"); An attacker submits: add-cart.php?num=1 UNION SELECT username, password FROM users-- add-cart.php num

$product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); $quantity = filter_input(INPUT_POST, 'quantity', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 99]]); if (!$product_id || !$quantity) http_response_code(400); die('Invalid request'); Always validate data types

Never trust user input. Always validate data types. Never use GET requests to modify state. And for the love of security, move away from raw add-cart.php scripts and toward modern, token-authenticated POST endpoints. token-authenticated POST endpoints.