【问题标题】:Undefined variable in Leaderboard(Unity and Php)排行榜中未定义的变量(Unity 和 Php)
【发布时间】:2017-03-23 07:35:42
【问题描述】:

所以这是我的 PHP 脚本,用于将分数从 unity 插入到 php

<?php
// create the connection to our database with following values: location of our databse
// (with xampp it's "localhost"), next is the login ("name" and "password").
// if the connection can not be established we get an error message, that we've entered after "or die"
$sql_connect = mysqli_connect("localhost", "id1151441_dbtest", "newcreator123") or die ("no DB Connection");

// after we're logged in, we can call our database
mysqli_select_db($sql_connect,"id1151441_dbtest123") or die ("DB not found");

// now we store our sent information from Unity in php variables, we can work with
if(isset(($_GET['newName']))){
    $name = $_GET['newName'];
}
if(isset($_GET['newScore'])){
    $score = $_GET['newScore'];
}

// Now we simply add/insert our values into our "highscores" table
// we first choose the columns and then add our values
// we don't need to fill in any value into the ID part, as it automatically gets a new value depending on the entries
mysqli_query($sql_connect,"INSERT INTO getdataofplayer (Name, Score) VALUES ($name,$score);");

// we're done now, so we can close the connection
mysqli_close($sql_connect);

?>

我有这个错误: 未定义变量:/storage/h10/441/1151441/public_html/InsertScore.php 第 21 行中的名称

在我的 wwwform Unity 脚本中是这样的

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HighScoreController : MonoBehaviour
{

    public string Name;
    public int Score;
    public string db_url = "http://testleaderboard.000webhostapp.com";
    public GameObject textScreen;

    void Update(){
        if(Input.GetKeyDown(KeyCode.Space)){
            SaveScore ();
        }
    }

    public void SaveScore(){
        StartCoroutine(SaveScores());
    }

    IEnumerator SaveScores(){
        // first we create a new WWWForm, that means a "post" command goes out to our database (for futher information just google "post" and "get" commands for html/php
        WWWForm form = new WWWForm();

        // with this line we will give a new name and save our score into that name
        // those "" indicate a string and attach the score after the comma to it
        form.AddField("newName", Name);
        form.AddField("newScore", Score);



        // the next line will start our php file that saves the Score and attaches the saved values from the "form" to it
        // For this tutorial I've used a new variable "db_url" that stores the path
        WWW webRequest = new WWW(db_url + "InsertScore.php", form);

        // with this line we'll wait until we get an info back
        yield return webRequest;
        if (webRequest.error != null) {
            Debug.Log (webRequest.error);
        } else {
            Debug.Log (webRequest.text);
        }
    }

    IEnumerator LoadScores(){
        // we don't need to store any variable in this, just run the php file
        WWW webRequest = new WWW(db_url + "index.php");

        // now we wait again for the feedback of the command
        yield return webRequest;

        // this is a GUIText that will display the scores in game.
        textScreen.GetComponent<Text>().text = webRequest.text;
        Debug.Log (webRequest);
    }
}

我已经被困了 5 个小时,只是因为我的 php 脚本上出现了这个未定义的变量错误,有人可以帮助我吗?提前谢谢你

【问题讨论】:

  • 您对SQL Injections 持开放态度,应该真正使用Prepared Statements,而不是连接您的查询。特别是因为您根本没有逃避用户输入!
  • 由于您在if--statements 中创建变量,它们未定义的唯一原因是if-statements 未验证为真。转储/记录 $_GET 变量并检查它包含的内容。在尝试更新数据库之前,您可能还应该添加一个检查以查看是否设置了两个变量。
  • @MagnusEriksson 我将其更新为 $name = mysqli_real_escape_string($sql_connect,$_GET['newName']); $score = mysqli_real_escape_string($sql_connect,$_GET['newScore']);
  • @MagnusEriksson 这是空先生。为什么它是空的?未定义的索引:/storage/h10/441/1151441/public_html/InsertScore.php 中的用户名第 25 行 NULL i var_dump($_GET['userName']);我明白了
  • 1. 您应该改用预处理语句(更安全)。 2. 除非newScore 是一个字符串,否则您不应该使用mysqli_real_escape_string(),因为那是用于...字符串。将分数转换为整数:$score = (int) $_GET['newScore'];。(使用准备好的语句时不需要)3。您需要在 SQL VALUES ('$name', ...) 中的字符串值周围加上单引号。 (使用准备好的语句时不需要)4. 如果值不存在,isset() 将验证为 false 为空。

标签: c# php unity3d


【解决方案1】:

您正在向表单添加数据

    WWWForm form = new WWWForm();

    form.AddField("newName", Name);
    form.AddField("newScore", Score);

    WWW webRequest = new WWW(db_url + "InsertScore.php", form);

您将其发布到网址。你的 PHP 应该是 $_POST['newName'],而不是 $_GET。您可以使用 $_GET 获取 URL 中的变量,就像您使用 WWW 一样:

new WWW(db_url + "InsertScore.php?newScore=10&newName=TheGinxx"

我不建议这样做,我建议您使用当前代码,但将您的 $_GETs 更改为 $_POSTs。

我也不完全确定sql语句是否会起作用,我通常用' '封装变量。那就是:

"INSERT INTO getdataofplayer (Name, Score) VALUES ('$name', '$score');"

对你来说,除非' 需要转义,但我不这么认为。我更喜欢总是对字符串使用',因为我听说它们的性能更高,因为它们不需要检查变量,但不要引用我的话。

编辑:

看来 OP 没有在 Unity 编辑器中设置变量。

【讨论】:

  • 它在我的 InsertScore.php 上仍然是未定义的索引,但在我的索引上,当我尝试这个新 WWW(db_url + "InsertScore.php?newScore=10&newName=TheGinxx"
  • 我不太明白,但我的回答建议您将 php 代码的 $_GET 更改为 $_POST 并使用您之前使用的代码。
  • 您得到“未设置变量”的原因是该变量未设置。您只是在创建变量if (isset($_GET['newName'])),这是错误的,因为您不是在发送 GET 变量,而是在发送 POST 变量。
  • if(isset(($_POST['newName']))){ $name = $_POST['newName']; } if(isset($_POST['newScore'])){ $score = $_POST['newScore']; } 像这个 ?先生已经试过了,但它仍然为空
  • 问题出在我的统一脚本中,先生。我很愚蠢,因为它说 null 因为它是空的 unity 。我太白痴了。谢谢先生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 2020-11-05
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
相关资源
最近更新 更多