D:\host\scoreman.in\highrange2026\update_result.php
<?php
// update_result.php — PHP 5.6 compatible
header('Content-Type: application/json; charset=UTF-8');
function json_exit($arr){
echo json_encode($arr);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
json_exit(array('ok'=>false, 'error'=>'Invalid method'));
}
require_once 'dbconnect.php'; // must define $con = mysqli connection
$appno = isset($_POST['appno']) ? trim($_POST['appno']) : '';
$event = isset($_POST['event']) ? trim($_POST['event']) : '';
$code = isset($_POST['code']) ? trim($_POST['code']) : '';
if ($appno === '' || $event === '' || $code === '') {
json_exit(array('ok'=>false, 'error'=>'Missing parameters'));
}
// Whitelist of updatable event columns
$allowed = array(
// Speed-Quad
'rink1','rink2','rink3','road1','road2',
// Speed-Inline
'rink4','rink5','rink6','rink7','p2p','elim','relay','mixedrelay','road3','road4','road5','p2prd','elimrd','marathon',
// Artistic
'figure','free','pair','solodance','pairdance','inlinefree','quartet','precisionskating','showgroup',
// Hockey
'quadhockey','mixedrollerhockey','inlinehockey',
// Freestyle
'speedslalom','classicslalom','pairslalom',
// Other Games
'alpine','downhill','rollerderby','rollerfreestylepark','skateboardingpark','scooterpark','rollerfreestylestreet','skateboardingstreet','scooterstreet'
);
if (!in_array($event, $allowed, true)) {
json_exit(array('ok'=>false, 'error'=>'Invalid event column'));
}
// Normalize result code
$uc = strtoupper($code);
// legacy medals
if ($uc === 'G') $uc = '1';
if ($uc === 'B') $uc = '3';
$allowedCodes = array('1','2','3','P','A','S','F'); // medals + Participated, Absent, Selected, Forward
if (!in_array($uc, $allowedCodes, true)) {
json_exit(array('ok'=>false, 'error'=>'Invalid result code'));
}
// Find app & repdistrict (and ensure eligible)
$repdistrict = '';
if ($st = mysqli_prepare($con, "SELECT repstate FROM applications WHERE appno=? AND order_status='Success' LIMIT 1")) {
mysqli_stmt_bind_param($st, "s", $appno);
mysqli_stmt_execute($st);
if (function_exists('mysqli_stmt_get_result')) {
$res = mysqli_stmt_get_result($st);
if ($row = mysqli_fetch_assoc($res)) { $repdistrict = (string)$row['repstate']; }
if ($res) mysqli_free_result($res);
} else {
mysqli_stmt_bind_result($st, $repdistrict);
mysqli_stmt_fetch($st);
}
mysqli_stmt_close($st);
}
if ($repdistrict === '') {
json_exit(array('ok'=>false, 'error'=>'Application not found or not eligible'));
}
// Check championships row for THIS district ONLY (no fallback)
$resultstatus = '';
$foundChamp = false;
if ($st = mysqli_prepare($con, "SELECT resultstatus FROM championships WHERE district=? ORDER BY id DESC LIMIT 1")) {
mysqli_stmt_bind_param($st, "s", $repdistrict);
mysqli_stmt_execute($st);
if (function_exists('mysqli_stmt_get_result')) {
$res = mysqli_stmt_get_result($st);
if ($row = mysqli_fetch_assoc($res)) {
$foundChamp = true;
$resultstatus = (string)$row['resultstatus'];
}
if ($res) mysqli_free_result($res);
} else {
mysqli_stmt_bind_result($st, $resultstatus);
if (mysqli_stmt_fetch($st)) { $foundChamp = true; }
}
mysqli_stmt_close($st);
}
if (!$foundChamp) {
json_exit(array('ok'=>false, 'error'=>'No championships row found for district: '.$repdistrict));
}
if (strcasecmp($resultstatus, 'Locked') === 0) {
json_exit(array('ok'=>false, 'error'=>'Results are locked for this district'));
}
// Perform update
$sql = "UPDATE applications SET `".$event."`=? WHERE appno=? LIMIT 1";
$st = mysqli_prepare($con, $sql);
if (!$st) {
json_exit(array('ok'=>false, 'error'=>'Prepare failed: '.mysqli_error($con)));
}
mysqli_stmt_bind_param($st, "ss", $uc, $appno);
$ok = mysqli_stmt_execute($st);
$err = mysqli_stmt_error($st);
$aff = mysqli_stmt_affected_rows($st);
mysqli_stmt_close($st);
if (!$ok) {
json_exit(array('ok'=>false, 'error'=>'Update failed: '.$err));
}
json_exit(array(
'ok'=>true,
'saved'=>array(
'appno'=>$appno,
'event'=>$event,
'code'=>$uc,
'affected'=>$aff
)
));