<?php
ob_start();

include_once '../includes/session.php';
include_once '../includes/constants.php';
include_once '../includes/functions.php';
include_once '../includes/allstripslashes.php';

// Connect to DB using modernized partners class
$partners = new partners;
$partners->connection($host, $user, $pass, $db);

$conn = $partners->conn;

// Sanitize input from POST
$nname = trim(filter_input(INPUT_POST, 'txtpayeename', FILTER_SANITIZE_STRING));
$acno  = trim(filter_input(INPUT_POST, 'txtacno', FILTER_SANITIZE_STRING));
$userid = 0; // This stays fixed as per your original logic

// Check if a record exists
$stmt = $conn->prepare("SELECT egold_id FROM partners_egold WHERE egold_user_id = ?");
$stmt->bind_param("i", $userid);
$stmt->execute();
$stmt->store_result();

if ($stmt->num_rows > 0) {
    // Update existing record
    $stmt->close();
    $update = $conn->prepare("UPDATE partners_egold SET egold_accno = ?, egold_payeename = ? WHERE egold_user_id = ?");
    $update->bind_param("ssi", $acno, $nname, $userid);
    $update->execute();
    $update->close();
} else {
    // Insert new record
    $stmt->close();
    $insert = $conn->prepare("INSERT INTO partners_egold (egold_accno, egold_payeename, egold_user_id) VALUES (?, ?, ?)");
    $insert->bind_param("ssi", $acno, $nname, $userid);
    $insert->execute();
    $insert->close();
}

// Redirect with message
$msg = urlencode("Update Successful");
header("Location: index.php?Act=payments&payid=5&mode=edit&msg=$msg");
exit;
?>
