<?php
include('include/session.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);
include_once('connection.php');
$dbConnection = new DbConnection();
$connection = $dbConnection->connection;

if(isset($_POST["submit"])){

    // Check if all required fields are set
    if(isset($_POST['product'], $_POST['variation'], $_POST['product_id'], $_POST['price'], $_POST['quantity'])) {
        $product = $_POST['product'];
        $variation = $_POST['variation'];
        $product_id = $_POST['product_id'];
        $price = $_POST['price'];
        $quantity = $_POST['quantity'];

        // Sanitize inputs to prevent SQL injection
        $product = mysqli_real_escape_string($connection, $product);
        $variation = mysqli_real_escape_string($connection, $variation);
        $product_id = mysqli_real_escape_string($connection, $product_id);
        $price = mysqli_real_escape_string($connection, $price);
        $quantity = mysqli_real_escape_string($connection, $quantity);

        // Corrected SQL query with proper formatting for the price value
        $query = "INSERT INTO product_variation (product, variation, product_id, price, quantity) VALUES ('$product', '$variation', '$product_id', '$price', '$quantity')";

        $query_run = mysqli_query($connection, $query);

        if($query_run){
            echo "<script> alert('Added Successfully'); window.location.href = '_add_stock.php'; </script>";
        } else {
            echo "<script> alert('Something went wrong');window.location.reload(); </script>";
        }
    } else {
        echo "<script> alert('Please fill all the fields');window.location.reload(); </script>";
    }
}
?>
