<?php
include "top.php"; // Assuming this contains your database connection

// Check if the update should be executed
if(isset($_GET['update']) && $_GET['update'] == '1') {
    // Get all users where custid is empty or null
    $query = "SELECT id, msoacno FROM adduser";
    $result = mysqli_query($con, $query);
    
    $updatedCount = 0;
    
    if(mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $id = $row['id'];
            $msoacno = $row['msoacno'];
            
            if(!empty($msoacno)) {
                // Update custid with msoacno value
                $updateQuery = "UPDATE adduser SET custid = '".mysqli_real_escape_string($con, $msoacno)."' WHERE id = $id";
                $updateResult = mysqli_query($con, $updateQuery);
                
                if($updateResult) {
                    $updatedCount++;
                }
            }
        }
        
        echo "<div class='success'>Successfully updated $updatedCount records.</div>";
    } else {
        echo "<div class='info'>No records need updating (all custid fields already have values).</div>";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Update Customer IDs</title>
    <style>
        body {
            font-family: Arial;
            width: 550px;
        }
        .outer-container {
            background: #F0F0F0;
            border: #e0dfdf 1px solid;
            padding: 40px 20px;
            border-radius: 2px;
        }
        .btn-submit {
            background: #333;
            border: #1d1d1d 1px solid;
            border-radius: 2px;
            color: #f0f0f0;
            cursor: pointer;
            padding: 5px 20px;
            font-size:0.9em;
        }
        .success {
            background: #c7efd9;
            border: #bbe2cd 1px solid;
            padding: 10px;
            margin-top: 10px;
            border-radius: 2px;
        }
        .info {
            background: #d9eaf7;
            border: #b8d4eb 1px solid;
            padding: 10px;
            margin-top: 10px;
            border-radius: 2px;
        }
    </style>
</head>
<body>
    <div class="outer-container">
        <h2>Update Customer IDs</h2>
        <p>This will update empty custid fields with msoacno values.</p>
        
        <form method="get">
            <input type="hidden" name="update" value="1">
            <button type="submit" class="btn-submit">Update Customer IDs</button>
        </form>
    </div>
</body>
</html>