<?php
include '../../function/connect.php';

$extplayer = $_POST['extplayer'];
$username  = trim($_POST['username']);

// basic validation
if ($username === '' || strlen($username) < 4) {
  header("Location: ../index.php?page=profile&pesan=username_invalid");
  exit;
}

// cek username sudah dipakai?
$cek = mysqli_query($koneksi,
  "SELECT id FROM tb_user WHERE username='$username' AND extplayer!='$extplayer' LIMIT 1"
);

if (mysqli_num_rows($cek) > 0) {
  header("Location: ../index.php?page=profile&pesan=username_used");
  exit;
}

// update
mysqli_query($koneksi,
  "UPDATE tb_user SET username='$username' WHERE extplayer='$extplayer'"
);

header("Location: ../index.php?page=profile&pesan=username_ok");