<?php 
include("dbConfig.php"); // Make sure this file connects to your database

if (isset($_POST['partId'])) {
    $partId = $_POST['partId'];

    // Prepare the SQL query to fetch UnitPrice based on the selected PartId
    $query = "SELECT `Price` FROM `particulars` WHERE `PartId` = ?";
    $stmt = $db->prepare($query);
    
    if ($stmt === false) {
        // Handle the error if preparation failed
        die('MySQL prepare error: ' . htmlspecialchars($db->error));
    }
    
    $stmt->bind_param("i", $partId);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($row = $result->fetch_assoc()) {
        echo $row['Price']; // Return the Price
    } else {
        echo "0"; // Return 0 if no price found
    }

    // Close the statement
    $stmt->close();
} else {
    echo "No PartId provided"; // Handle case when no PartId is sent
}

$db->close(); // Always close the database connection
?>
