<?php
include 'includes/config.php';


// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve and sanitize form inputs
    $businessId = (int)$_POST['business_id'];
    $name = trim($_POST['name']);
    $description = trim($_POST['description']);

    // Validate required fields
    if (empty($name) || empty($description)) {
        die("Please fill all required fields.");
    }

    // Prepare the SQL query to insert data
    $sql = "INSERT INTO `brands` (`business_id`, `name`, `description`) VALUES (:business_id, :name, :description)";
    $stmt = $conn->prepare($sql);

    // Bind parameters
    $stmt->bindParam(':business_id', $businessId, PDO::PARAM_INT);
    $stmt->bindParam(':name', $name, PDO::PARAM_STR);
    $stmt->bindParam(':description', $description, PDO::PARAM_STR);

    try {
        // Execute the query
        $stmt->execute();

        echo "<script>alert('Unit successfully added!');</script>";
        echo "<script>window.location.href='brand.php'</script>";
        
    } catch (PDOException $e) {
        die("Insertion failed: " . $e->getMessage());
    }
}
?>
