<?php
// get_user_credit.php

include('connection.php');
if (isset($_POST['user_id'])) {
  $userId = $_POST['user_id'];
  
  // Perform a database query to retrieve credit and virtual_credit for the specified user_id
  $sql = "SELECT credit, virtual_credit FROM credit WHERE user_id = '$userId'";
  $res = mysqli_query($conn, $sql);
  
  if ($row = mysqli_fetch_assoc($res)) {
    // If the database query returns a valid result, create an array with the credit and virtual_credit values
    $response = array(
      'success' => true,
      'credit' => $row['credit'],
      'virtual_credit' => $row['virtual_credit']
    );
  } else {
    // If the database query doesn't return any result (no matching user_id found), set success to false
    $response = array('success' => false);
  }
} else {
  // If the user_id is not provided in the POST data, set success to false
  $response = array('success' => false);
}

// Return the response as JSON
echo json_encode($response);
?>
