0

I am using the following code to send data from my application to a PHP script on my server:

postList.add(new BasicNameValuePair("PType", TxtWeight.getText().toString()));

String StrURL="http://chacho.comuv.com/android/insert_pistachio_sell.php";
myJsonObject=JP.store_And_Feedback("PType",SpnPistachio.getSelectedItem().toString()));
DefaultHttpClient DHC=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("StrURL");
httpPost.setEntity(new UrlEncodedFormEntity(postList));

The following PHP code will store the received data in my database:

<?php
    require_once "./includes/DB_Connect.php"
    $PType=$_POST['PType'];

    $SQL="INSERT INTO  sell (ptype)VALUES ('".mysql_real_escape_string($PType)."')";
    $Result=mysql_query($SQL);
?>

But data is saved in the database like "??????????"

I have searched a lot but I have not found a way to make this work properly.

4
  • I don't see in your code that you add the Ptype key-value pair to the postList list. Are you writing the PWeight to the database? If so does that get sent via the POST request correctly? Commented Jan 7, 2014 at 19:16
  • it was just a typeing problem;i correct it Commented Jan 7, 2014 at 19:38
  • Are all characters being converted to ? or just non-ASCII ones? For consistent Unicode support you should use UTF-8 on both sides: use new UrlEncodedFormEntity(postList, "UTF-8") at the client, mysql_set_charset('utf-8') at the app server and UTF-8-collated tables at the db server. Commented Jan 7, 2014 at 23:37
  • thank you a lot @bobince; new UrlEncodedFormEntity(postList, "UTF-8") slove my Problem and every thing work OK Commented Jan 8, 2014 at 4:30

1 Answer 1

2

first i'm thank you from @bobince for help

1- in mysql set collection of field to "utf8_persian_ci"

2- in php code after connect to db run this code

mysql_query("SET NAMES 'UTF8'");

3- in android code use UTF-8 encode like this code

new UrlEncodedFormEntity(postList, "UTF-8")

data will save correctly on your mysql database

Sign up to request clarification or add additional context in comments.

3 Comments

Prefer mysql_set_charset('utf-8') over SET NAMES. The latter does not correctly update the encoding information used by mysql_real_escape_string, making it effectively the same as mysql_escape_string.
@bobince thank you again;i am just use mysql_query("SET NAMES 'UTF8'"); in php script and use new UrlEncodedFormEntity(postList, "UTF-8") in android application and remove mysql_real_escape_string from php script; every thing work perfect :)
Don't remove mysql_real_escape_string, you need it to avoid SQL injection vulnerabilities! (In general, you are better off using mysqli or PDO together with parameterised queries so that you don't have to use mysql_real_escape_string explicitly each time, but if you are sticking with mysql then this SQL-escaping is essential.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.