<?php
include("connDb.php");

try {

    $fields = [
        'fidCurDate',fidTot,
        'at420', 'ai163', 'ar091', 'mi866', 'mi543',
        'mt592', 'as551', 'mTot', 'nTot', 'oTot',
        'ed5318', 'ed5319', 'ed5320', 'ed5321',
        'dt683', 'dt214', 'aTot', 'bTot', 'cTot', 'dTot', 'eTot',
        'fidComment', 'djiaChange'
    ];

    $data = [];
    foreach ($fields as $field) {
        $data[$field] = $_POST[$field] ?? 0;
    }
    $data['fidCurDate']= "'" . $_POST['fidCurDate'] . "'";
    $data['fidComment']= "'" . $_POST['fidComment'] . "'";

    $placeholders = implode(', ', array_fill(0, count($fields), '?'));
    $columns = implode(', ', $fields);
    $sqlCmd = "INSERT INTO fidelity ($columns) VALUES ($placeholders)";

    // Optional debug
    // var_dump($sqlCmd); 
    // print_r($data);

    $stmt = $pdo->prepare($sqlCmd);
    $stmt->execute(array_values($data));

    echo "****  Record inserted successfully ****";

} catch (PDOException $e) {
    http_response_code(500);
    echo "ERROR: Database error: " . $e->getMessage();
}
?>

