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 25, 2014 at 07:55 AM · c#php

Why the Value of form.AddField won't accept string

I want to send some data to my server.. so i make this C# code

 IEnumerator SaveAllPlayerPrefs(string[] parms)
     {
         WWWForm form = new WWWForm();
 
         form.AddField("bone" , value);
 
         WWW webRequest = new WWW(db_url + "SaveAllPlayerPref.php", form);
 
         yield return webRequest;
     }


And i have php code for recieving that

 <?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);
 ?>


The problem is when i try this code it works nicely, but it when the value is still int. when i change the value into string type, it can't send to database, and my unity is crash,

later i try to change the value to char, i try to send

'a'

but when i do that in database its not saving 'a' instead its saving 97, the ASCI value.. I dont't understand what's wrong with this, and in my database in server, i set the value is varchar(100).. please help me and 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 HarshadK · Nov 25, 2014 at 08:18 AM 0
Share

Off topic but important to note:

The mysql extension in deprecated hence you should not use it. Ins$$anonymous$$d use $$anonymous$$ySQLi or PDO_$$anonymous$$ySQL extension. Also mysql extension will be removed in future.

Refer Choosing an API from PHP website.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Uldeim · Nov 25, 2014 at 10:34 AM

I can't imagine why Unity would crash, but I can understand why your PHP code would fail. When you're using SQL, strings are required to be surrounded by single quotes, and you're neither escaping nor cleaning your input.

What you have is:

mysql_query("INSERT INTO save_game (bone) VALUES ($bone);");

but what you need is:

mysql_query("INSERT INTO save_game (bone) VALUES ('$bone');");

Note the addition of single quotes surrounding the variable.

I presume that when the PHP code encountered a fatal error and threw an Exception, it either output nothing, causing Unity to hang, or output an Exception, which Unity caught and rethrew.

A couple of side notes:

-The reason sending a char worked is because chars are represented as small integers internally. You were using the (varName, int) version of the command instead of the (varName, string) one.

-You should almost certainly clean your input strings before inserting them into your database. This means you need to check them for single quotes and semi-colons at the very least. Why? Because if someone (somehow) put the value of "');DELETE FROM save_game WHERE ('1=1" or something similar, you don't want that to get run as it is. Look into PDO as HasrhadK mentions; this does it for you automatically if you use the right methods.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to send data form to server using json in unity 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Put user list in 2d array 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