Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Flash Game Scores PHP/MySQL

I need to save highscores of a Flash game using PHP/MySQL but it's not working

Here is the code:

AS3

ActionScript :

var myrequest:URLRequest = new URLRequest("score.php");
myrequest.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.name = nombrem;
variables.score = puntosJugador;
myrequest.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myrequest);

PHP

//Include database connection details
require_once('config.php');
//Connect to mysql server
$link = mysql_connect([b]**HOST**, **USERNAME**, **PASSWORD**[/b]D);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(**DATABASENA**);
if(!$db) {
die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}

//Sanitize the POST values
$name = clean($_POST['username']);
$score = clean($_POST['score']);
$currentdate = date("Y/m/d");

//Create INSERT query
$qry = "INSERT INTO highscores(user,time,date) VALUES('$name','$score','$currentdate')";
$result = @mysql_query($qry);
echo "writing=Ok";
exit();
mysql_close();
?>

and I created a table on MySQL named 'highscores' with names 'user', 'time' and 'date'

any help please?

like image 254
Abel Abad Avatar asked Nov 01 '22 13:11

Abel Abad


1 Answers

you are sending "name" but trying to get it by "username" ($_POST['username'])

also do mysql_close(); before exit(); (in generally, you don't need exit() by the way).

like image 77
tanaydin Avatar answered Nov 15 '22 07:11

tanaydin