0

How can i save a javascript variable into MYSQL database? In particular i want to save a score of a game, when it ends. See the code below:

function lastScore(num){
    document.getElementById('lastscore').textContent = num;
}


function saveScore(){
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET","saveScore.php?s="+player.score,false);
    xmlhttp.send(null);
    document.getElementById("endgame").innerHTML = xmlhttp.responseText;
}

PHP

<?php
include('server.php');

$s =  $_GET['s'];
$db = mysqli_connect( ...MY DATA...);

$query = "INSERT INTO scores (name, score) VALUES ('$username' , '$s')";
mysqli_query($db, $query);
?>

Can you tell me what's wrong? what should i do to be able to insert player.score as argument into MYSQL database? Thanks

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46

0 Answers0