Ajax Error For PHP Request ?

1 favourites
  • 6 posts
From the Asset Store
Use this game pack to create your own game, modify the existing game or simply take a look and see how it was made.
  • <?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

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Press F12, open browser console and you'll see the error message. If it's related to CORS, search the forum, there are lots of topics about it.

  • Hi. I am adding such CORS settings in php on server.

    //Allowing the browser to make crossdomain requests
    @header('Access-Control-Allow-Origin: *');
    @header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    @header("Content-Type: text/html; charset=UTF-8");
    
  • i changed the php script

    now no any error and browser console and data submitted successfully from c3 project

    but not reflecting in sql table

    table name - users

    name and score

    <?php

    header("Access-Control-Allow-Origin: *");

    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");

    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

    // Database connection details

    $db = "id22078763_store"; // Your database name

    $dbu = "id22078763_db"; // Your database username

    $dbp = "Db@user5"; // Your database users' password

    $host = "localhost"; // MySQL server - usually localhost

    // Connect to the database

    $dblink = mysqli_connect($host, $dbu, $dbp, $db);

    // Check the connection

    if (!$dblink) {

    die("Connection failed: " . mysqli_connect_error());

    }

    if (isset($_GET['name']) && isset($_GET['score'])) {

    // Lightly sanitize the GET parameters

    $name = mysqli_real_escape_string($dblink, $_GET['name']);

    $score = mysqli_real_escape_string($dblink, $_GET['score']);

    // Insert the score into the database

    $sql = "INSERT INTO `users` (`name`, `score`) VALUES ('$name', '$score')";

    if (mysqli_query($dblink, $sql)) {

    echo 'Your score was saved. Congrats!';

    } else {

    // Log or echo the error message if the query fails

    echo 'There was a problem saving your score. Please try again later. Error: ' . mysqli_error($dblink);

    }

    } else {

    echo 'Your name or score was not passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the URL.';

    }

    // Close the MySQL connection

    mysqli_close($dblink);

    ?>

  • Here is my script to save and load the game for each player. It works, but I created it a long time ago, maybe there are newer methods.

    <?php

    //

    $mysql_host = "xxxxx";

    $mysql_database = "xxxxx";

    $mysql_user = "xxxxx";

    $mysql_password = "xxxxx";

    //

    $link = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("Err MySQL" );

    mysql_select_db($mysql_database, $link) or die ('err db');

    //

    Header('Access-Control-Allow-Origin: *');

    Header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

    Header("Content-Type: text/html; charset=UTF-8");

    $to = $_POST['to'];

    //

    if($to == 'savegame'){

    $id_user = $_POST['id_user']; //

    $gamedata = $_POST['gamedata']; //

    //

    $q1 = mysql_query("SELECT * FROM savegames WHERE id_user='".$id_user."' ORDER BY id_user DESC LIMIT 1");

    if(mysql_num_rows($q1)==0){

    //

    mysql_query("INSERT INTO savegames VALUES (NULL,'".$id_user."','".$gamedata."')");

    } else {

    //

    mysql_query("UPDATE savegames SET gamedata='".$gamedata."' WHERE id_user='".$id_user."'");

    }

    echo 'ok';

    }

    //

    if($to == 'loadgame'){

    $id_user = $_POST['id_user']; //

    //

    $q1 = mysql_query("SELECT * FROM savegames WHERE id_user='".$id_user."' ORDER BY id_user DESC LIMIT 1");

    if(mysql_num_rows($q1)==0){

    //

    echo 'error';

    } else {

    //

    $q2 = mysql_fetch_array($q1);

    echo $q2['gamedata'];

    }

    }

  • you won bro

    and i got it...

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)