setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $customerId = $_POST['customerId']; $totalAmount = $_POST['totalAmount']; $tenure = min($_POST['tenure'], 18); // Limit tenure to 18 months $firstEmiDate = $_POST['firstEmiDate']; $emiAmount = $totalAmount / $tenure; // Prepare the query dynamically $columns = ['customerId', 'totalAmount', 'tenure', 'emiAmount', 'firstEmiDate']; $placeholders = [':customerId', ':totalAmount', ':tenure', ':emiAmount', ':firstEmiDate']; $params = [ ':customerId' => $customerId, ':totalAmount' => $totalAmount, ':tenure' => $tenure, ':emiAmount' => $emiAmount, ':firstEmiDate' => $firstEmiDate ]; // Add EMI columns dynamically based on tenure for ($i = 1; $i <= $tenure; $i++) { $emiDate = date('Y-m-d', strtotime("+$i months", strtotime($firstEmiDate))); $columns[] = "emi$i"; $columns[] = "emi{$i}Date"; $placeholders[] = ":emi$i"; $placeholders[] = ":emi{$i}Date"; $params[":emi$i"] = $emiAmount; $params[":emi{$i}Date"] = $emiDate; } // Construct SQL statement $sql = "INSERT INTO billing (" . implode(',', $columns) . ") VALUES (" . implode(',', $placeholders) . ")"; $stmt = $db->prepare($sql); // Execute the query if ($stmt->execute($params)) { echo '
Name: = htmlspecialchars($customer['name']) ?>
Mobile: = htmlspecialchars($customer['mobile']) ?>