<?php
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
// Database connection details
$servername = "localhost";
$username = "id22078763_db";
$password = "Db@user5";
$dbname = "id22078763_store";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if the request method is POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the score and player name from the POST data
$score = $_POST['score'];
$playerName = $_POST['playerName'];
// Check if the player exists
$sql = "SELECT id FROM users WHERE name = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $playerName);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Player exists, update their score
$updateSql = "UPDATE users SET score = ? WHERE name = ?";
$updateStmt = $conn->prepare($updateSql);
$updateStmt->bind_param("is", $score, $playerName);
$updateStmt->execute();
echo "Score updated successfully";
} else {
// Player doesn't exist, insert a new record
$insertSql = "INSERT INTO users (name, score) VALUES (?, ?)";
$insertStmt = $conn->prepare($insertSql);
$insertStmt->bind_param("si", $playerName, $score);
$insertStmt->execute();
echo "New record created successfully";
}
$stmt->close();
}
// Close the database connection
$conn->close();
?>
On Sql I have
users table
in this table
name and score
and im trying to post score
getting error
i dont know what's going wrong