<?php
include("connection.php");
if (isset($_POST['update'])) {
    $wallet_address = $_POST['wallet_address'];
    $network = $_POST['network'];
    $id = $_POST['id'];
    // Check if a new file is uploaded
    if ($_FILES['usdt_qr']['size'] > 0) {
        $usdt_qr = $_FILES['usdt_qr']['name'];
        $usdt_qr_temp = $_FILES['usdt_qr']['tmp_name'];
        $usdt_qr_destination = 'img/' . $usdt_qr;

        // Update the existing record, including the usdt_qr field
        $updateQuery = "UPDATE usdt_payment SET wallet_address = '$wallet_address', network = '$network', usdt_qr = '$usdt_qr' WHERE id='$id'";
        $updateResult = mysqli_query($conn, $updateQuery);

        if ($updateResult) {
            move_uploaded_file($usdt_qr_temp, $usdt_qr_destination);
            header("Location: usdt_payment.php?msg=1"); // Updated successfully
            exit;
        } else {
            header("Location: usdt_payment.php?msg=2"); // Failed to update
            exit;
        }
    } else {
        // No new file uploaded, update the record without changing usdt_qr field
        $updateQuery = "UPDATE usdt_payment SET wallet_address = '$wallet_address', network = '$network' WHERE id='$id'";
        $updateResult = mysqli_query($conn, $updateQuery);

        if ($updateResult) {
            header("Location: usdt_payment.php?msg=1"); // Updated successfully
            exit;
        } else {
            header("Location: usdt_payment.php?msg=2"); // Failed to update
            exit;
        }
    }
} else {
    header("Location: usdt_payment.php?msg=2"); // Failed to insert
    exit;
}
?>
