最初我想使用node.js,但经过一整天的挫折之后,我转而使用jquery和mySQL。登录似乎正在起作用,但处理变量的方式有问题。我想要做的就是用两件事来更新数据库:得分和名称。这是我在PHP中为我的项目修改的代码:
<?php
$db = "myDatabaseNameIsCorrect";//Your database name
$dbu = "soIsMyUsername";//Your database username
$dbp = "AndMyPassword";//Your database users' password
$host = "localhost";//MySQL server - usually localhost
$dblink = mysql_connect($host,$dbu,$dbp);
$seldb = mysql_select_db($db);
if(isset($_GET['name']) && isset($_GET['this.score'])){
//Lightly sanitize the GET's to prevent SQL injections and possible XSS attacks
$name = strip_tags(mysql_real_escape_string($_GET['name']));
$score = strip_tags(mysql_real_escape_string($_GET['this.score']));
$sql = mysql_query("INSERT INTO `$db`.`scores` (`id`,`name`,`score`) VALUES ('','$name','$score');");
if($sql){
//The query returned true - now do whatever you like here.
echo 'Your score was saved. Congrats!';
}else{
//The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
echo 'There was a problem saving your score. Please try again later.';
}
}else{
echo 'Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.';
}
mysql_close($dblink);//Close off the MySQL connection to save resources.
?>
这是JS!运行PHP:
let gameoverScene = new Phaser.Scene('GameOver');
gameoverScene.create = function(){
this.laughSound=this.sound.add('laughSound')
this.gameW = this.sys.game.config.width;
this.gameH = this.sys.game.config.height;
this.goToTitle=function(){
var name = prompt('Enter your name');
jQuery.ajax({
type: "POST",
url: 'savescores.php?name=' +name +'&score=' + this.score,
dataType: 'text',
data: {functionname: 'add', arguments: [name, this.score]},
success: function (obj, textstatus) {
if( !('error' in obj) ) {
yourVariable = obj.result;
}
else {
console.log(obj.error);
}
}
});
this.scene.start('Title')
};
我也尝试过更改数据类型,但是没有用,但我还没有把它排除在外。
以下是项目和数据库的链接:
呜呜呜.IgG了PU底.com/deer defender/testing
呜呜呜.IgG了PU底.com/deer defender/testing/get scores.PHP
这是我得到的错误:
gameover.js:20 Uncaught TypeError: Cannot use 'in' operator to search for 'error' in
Your name or score wasnt passed in the request. Make sure you add ?name=NAME_HERE&score=1337 to the tags.
at Object.success (gameover.js:20)
at fire (jquery.js:3268)
at Object.fireWith [as resolveWith] (jquery.js:3398)
at done (jquery.js:9305)
at XMLHttpRequest.<anonymous> (jquery.js:9548)
所以,你得到的错误是因为,在JavaScript中,obj(或obj的位置中的参数)is a string,而不是数组。
你可以看到一些关于如何正确检查和捕获错误的here示例。
编辑:那么,关于你关于得分变量的问题。
值得注意的是,这里有两种类型的变量。
第一个是PHP GET变量。 PHP GET变量通过以下格式设置:
var=value
您可以通过调用这样的PHP脚本来设置这些变量:
script.php?var1=value1&var2=value2&var3=value3 // etc...
您可以像这样访问它们:
echo $_GET["var1"];
echo $_GET["var2"];
echo $_GET["var3"];
产生结果:
value1
value2
value3
播放的第二个变量是JavaScript变量。这些只能在JavaScript中访问。 JavaScript变量在PHP中没有任何意义。
那么,让我们来看看你在JavaScript中做了些什么:
url: 'savescores.php?name=' +name +'&score=' + this.score,
为了解释让我们说name = Chipster,而this.score = 123。
这段代码将尝试打开以下文件:
savescores.php?name=Chipster&score=123
记住PHP GET变量是通过格式script.php?var1=value1&var2=value2&var3=value3 // etc...
设置的,我们可以看到PHP脚本中有2个GET变量,名为name和score。因此,要从PHP访问分数,您需要这样做:
echo $_GET["score"];
这将在我们的示例中输出123
。
这可能无法解决您的问题,但我在您的代码中看到的一个问题是调用strip_tags(或另一个改变字符串的函数)后,它已被引用以插入mysql_real_escape_string可能会破坏mysql_real_escape_string的目的。它应该是在插入数据之前调用数据的最后一个函数。
此外,如果score是整数字符串,则intval与mysql_real_escape_string一样用于清理整数以进行插入。
编辑:当jQuery中使用的提交方法是POST时,您还在PHP中检查GET变量。尝试在PHP端查看$ _POST而不是$ _GET。如果您通过POST将变量放入请求体中,则无需将变量放入查询字符串中。