• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by steb92 · Mar 23, 2015 at 03:19 AM · c#databasemysqlphpleaderboard

Saving a leaderboard using PHP and C#.

Hello all. I'm trying to save my leaderboard to a MySQL database. I'm using the XAMPP package and am running Apache and MySQL as I type. The php file is located in E:\xampp\htdocs\hunted_vr\scores.php. The database contains one table (score_details) and three columns. player_id (an AI primary key), player_name and player_score.

The scores.php reads:

 <?php
 
  $pn = $_REQUEST['player_name'];
  
  $ps = $_REQUEST['player_score'];
  
  @ $db = new mysqli('localhost', 'root', 'root', 'bot');
             if (mysqli_connect_errno()) {
                 echo 'Error: Could not connect to database.  Please try again later.';
                 exit;
             }
     $q1 = "INSERT INTO `leaderboard`.`score_details` (`player_id`, `player_name`, `player_score`) VALUES (NULL, '$pn', '$ps');";    
 
 ?>

My C# is attached to an empty game object in my scene. It is located in my game folder, which is somewhere in E:\Documents\Hunted_VR\Game Assets\Scripts\

It reads:

 using UnityEngine;
 using System.Collections;
 
 public class SendScore : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
 
         PostScore("Test", 100);
     }
 
     private void PostScore(string name, int score)
     {
         string url = "localhost/hunted_vr/scores.php";
 
         WWWForm form = new WWWForm();
         form.AddField("name", name);
         form.AddField("score", score.ToString());
         WWW www = new WWW(url, form);
 
         StartCoroutine(WaitForRequest(www));
     }
     
     IEnumerator WaitForRequest(WWW www)
     {
         yield return www;
  
     // check for errors
         if (www.error == null)
         {
             Debug.Log("WWW Ok!: " + www.text);
         } 
         else 
         {
             Debug.Log("WWW Error: "+ www.error);
         }
     }  
 }
 

Any and all help appreciated. Currently the game compiles and runs, and my C# check for errors returns 'WWW Ok!' but the php and C# do not seem to be communicating together.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shoni-wheeler · Mar 23, 2015 at 11:47 PM 0
Share

Well looking at your code you are connecting to a db named "bot" and inserting into a table named "leaderboard.score_details"

John

avatar image steb92 · Mar 24, 2015 at 09:24 AM 0
Share

Hi John, thanks for spotting that. Sadly, I've corrected it now and still my problem persists.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by NomiDan · Mar 31, 2015 at 04:26 PM

Hi, Did you include the crossdomain.xml file at the root of your server? Read about it on http://docs.unity3d.com/Manual/SecuritySandbox.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by apple92 · May 29, 2015 at 09:23 AM

I think you are missing the Execution of the Query. You created a Query but never executed it.

It's also worth mentioning, that you should do some more validation on the Data. use htmlentities or htmlspecialchars to strip away characters that could allow Injection. You should probably also read a few articles about Code Injection in php/MYSQL. Could help you to keep your Database safe. Though Prepared Statements are already a Huge help. And you should really use them. Very important for Security.

 <?php
  
   $pn = $_REQUEST['player_name'];
   
   $ps = $_REQUEST['player_score'];
   
   @ $db = new mysqli('localhost', 'root', 'root', 'bot'); // "localhost","my_user","my_password","my_db"
              if (mysqli_connect_errno()) {
                  echo 'Error: Could not connect to database.  Please try again later.';
                  exit;
              }
      $q1 = "INSERT INTO `leaderboard`.`score_details` (`player_id`, `player_name`, `player_score`) VALUES (NULL, '$pn', '$ps')"; // removed the ; inside SQL Command   
 
      mysqli_query($db, $q1); // Query it using the connection you've made and the Query you've constructed.
  ?>
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

WebHosting with phpMyAdmin PHP 0 Answers

How to make a highscore database for unity? 1 Answer

How to get Variable from Php Script? 0 Answers

Login from database in unity 0 Answers

WWW form isn't working in Build 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges