I am making a little website game for class and we have to link options to a form that sends the data to a database through PHP. I have managed to link the database fine for all of the aspects of the game except upating the highscore.
Here is the JS code:
function new_score(score) {
// set up an XMLHttpRequest object
var connect = new XMLHttpRequest();
connect.responseType = "json";
// run server side query
var name = document.getElementById("lookup_name_selector").value;
var query ="update_highscore.php?name=" + name + "&highscore=" + score;
connect.open("GET", query);
connect.send();
}
and here is the php file:
require 'get_db_handle_path.php';
require GET_DB_HANDLE_PATH."/get_db_handle.php";
$conn = get_db_handle();
// prepared statements help prevent SQL injection attacks
// if you have to run them lots of times, they're also faster
$stmt = $conn -> prepare();
$stmt = $conn ->
prepare("UPDATE favourites SET highscore=:score WHERE name=:name".
"values (:name, :score)");
$stmt -> bindParam(':name', $_GET['name']);
$stmt -> bindParam(':score', $_GET['highscore']);
$stmt -> execute();
// no need for a loop, since there is at most one result
// 'name' is primary key for the table favourites
echo json_encode($stmt -> fetch(PDO::FETCH_ASSOC));
// remove all references to the connection
$stmt = null;
$conn = null;
thanks in advanced!