duanletao9487 2019-01-31 05:04
浏览 81
已采纳

我在PHP中处理这些变量的方式有什么问题?

Originally I wanted to use node.js, but after an entire day of frustration, I switched to using jquery and mySQL. The logins seem to be working, but something is wrong in the way it is handling variables. All I want to do is update the database with two things: score and name. Here is the code I modded for my project in 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.
?> 

And here is the JS! that runs the 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')
    };

I also tried changing the data type and that didn't work, but I'm not ruling it out yet as a problem.

Here are links to the project and the database:

www.igglepud.com/DeerDefender/Testing

www.igglepud.com/DeerDefender/Testing/getscores.php

This is the error I get:

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)
  • 写回答

2条回答 默认 最新

  • dqtdz08206 2019-01-31 06:28
    关注

    So, the error you're getting is because, in the JavaScript, obj (or the parameter in obj's position) is a string, not an array.

    You can see some examples here of how you can properly check for and catch errors.

    Edit: So, in regards to your question about the score variable.

    It's important to note that there are 2 types of variables at play here.

    The first one is PHP GET variables. PHP GET variables are set via the following format:

    var=value
    

    You can set these variables by calling a PHP script like this:

    script.php?var1=value1&var2=value2&var3=value3 // etc...
    

    You can access them like this:

    echo $_GET["var1"];
    echo $_GET["var2"];
    echo $_GET["var3"];
    

    Which produces the result:

    value1
    value2
    value3
    

    The second variable at play is a JavaScript variable. Those can only be accessed in JavaScript. a JavaScript variable means nothing in PHP.

    So, let's examine what you're doing from the JavaScript:

    url: 'savescores.php?name=' +name +'&score=' + this.score,
    

    For the purpose of explaining let's say name = Chipster, and this.score = 123.

    What this code will do is try to open the following file:

    savescores.php?name=Chipster&score=123
    

    Remembering that PHP GET variables are set by the format script.php?var1=value1&var2=value2&var3=value3 // etc... we can see that there are 2 GET variables available from the PHP script called name and score. Thus, to access score from PHP you need to do it like this:

    echo $_GET["score"];
    

    This will output 123 in our example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab