Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 folta20 · Nov 26, 2014 at 02:17 PM · c#jsonphp

How to send data form to server using json in unity

I want to using json to send the form to my server here is my C# code

 public string db_url="http://localhost/";
     IEnumerator SaveAllPlayerPrefs(object[] parms)
         {
             string ourPostData = "{\"bone\":\"42\"}";
             
             Hashtable headers = new Hashtable();
             headers.Add("Content-Type", "application/json");
             headers.Add("Cookie", "Our session cookie");
             
             byte[] pData = System.Text.Encoding.UTF8.GetBytes(ourPostData);
     
             WWW webRequest = new WWW(db_url + "SaveAllPlayerPref.php", pData, headers);
     
             yield return webRequest;
         }
 


and here is my php code:

 <?php
     $sql_connect = mysql_connect("localhost", "root", "") or die ("no DB Connection");
     
  mysql_select_db("example") or die ("DB not found");
     
     $bone = $_POST['bone'];  
     
     mysql_query("INSERT INTO save_game (bone) VALUES ('$bone');");
 
     mysql_close($sql_connect);
 ?>

When i run it this code its running, but when i check the database, its not save the value of bone. It's save the empty string to bone, i want it to save 42.. in my database bone is varchar(100) and utf8_general_ci.

Can someone explain to me? thx before:)

Comment
Add comment · Show 1
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 Azrapse · Nov 30, 2016 at 08:48 AM 0
Share

$bone = $_POST['bone'];

After this line of code is run in the server, what is the content of the variable $bone? Have you checked? I suspect it's null. Don't you have to use json_decode or something in PHP?

If that is the case, this is definitely not a Unity question.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by kmfjsc · Nov 30, 2016 at 08:10 AM

I've done for doing this below. Let's go : ==>

 using UnityEngine;
 
 using UnityEngine.UI;
 
 using System.Collections;
 
 using System.Collections.Generic;
 
 public class btnGetData : MonoBehaviour {
 
  void Start()
  {
      gameObject.GetComponent<Button>().onClick.AddListener(TaskOnClick);
  }
  IEnumerator WaitForWWW(WWW www)
  {
      yield return www;
     
      
      string txt = "";
      if (string.IsNullOrEmpty(www.error))
          txt = www.text;  //text of success
      else
          txt = www.error;  //error
      GameObject.Find("Txtdemo").GetComponent<Text>().text =  "++++++\n\n" + txt;
  }
  void TaskOnClick()
  {
      try
      {
          GameObject.Find("Txtdemo").GetComponent<Text>().text = "starting..";   
          string ourPostData = "{\"plan\":\"TESTA02\"";
          Dictionary<string,string> headers = new Dictionary<string, string>();
          headers.Add("Content-Type", "application/json");
          //byte[] b = System.Text.Encoding.UTF8.GetBytes();
          byte[] pData = System.Text.Encoding.ASCII.GetBytes(ourPostData.ToCharArray());
          ///POST by IIS hosting...
          WWW api = new WWW("http://192.168.1.120/si_aoi/api/total", pData, headers);
          ///GET by IIS hosting...
          ///WWW api = new WWW("http://192.168.1.120/si_aoi/api/total?dynamix={\"plan\":\"TESTA02\"");
          StartCoroutine(WaitForWWW(api));
      }
      catch (UnityException ex) { Debug.Log(ex.Message); }
  }
  
 }
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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

JSON vs XML for Unity C# 1 Answer

jSON data error Unity3d 0 Answers

Error ambiguous reference with Json 1 Answer

A node in a childnode? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges