<?php
require_once dirname(__DIR__) . '/auth_check.php';
require_once dirname(__DIR__) . '/config.php';

$id       = isset($_POST['id'])       ? (int)$_POST['id'] : 0;
$username = isset($_POST['username']) ? mysqli_real_escape_string($koneksi, trim($_POST['username'])) : '';
$email    = isset($_POST['email'])    ? mysqli_real_escape_string($koneksi, trim($_POST['email']))    : '';
$cr       = isset($_POST['cr'])       ? (int)$_POST['cr'] : 0;
$expired  = isset($_POST['expired'])  ? mysqli_real_escape_string($koneksi, trim($_POST['expired'])) : '';
$status   = isset($_POST['status'])   ? mysqli_real_escape_string($koneksi, trim($_POST['status']))  : '';
$info     = isset($_POST['info'])     ? mysqli_real_escape_string($koneksi, trim($_POST['info']))    : '';
$password = isset($_POST['password']) ? trim($_POST['password']) : '';

if ($id <= 0 || $username === '' || $email === '') {
    echo "error: Missing required fields"; die();
}

$passUpdate = '';
if ($password !== '') {
    $ep = mysqli_real_escape_string($koneksi, $password);
    $passUpdate = ", password='$ep'";
}

$sql = "UPDATE user_id SET username='$username', email='$email', cr=$cr, expired='$expired', status='$status', info='$info'$passUpdate WHERE ID=$id";
if (mysqli_query($koneksi, $sql)) {
    echo "success";
} else {
    echo "error: " . mysqli_error($koneksi);
}
