0

i am new android developer who developing a persian real estate android project.i have a form which inserts new estate to mysqli table using php file.but when i use persian characters in every field of that form it actually inserts null values to every fields of table.but when i use english characters it inserts to the table completely! for example here is my english characters json sent from android :

{"price":"5668845555","address":"ssfghhvcxdd","phoneNumber":"544585533","bedRoom":6,"describtion":"fdstghgccff","isForSale":"rent","isCommertial":"not commertial","meter":545,"image":"android.graphics.Bitmap@c10755a"}

and this is included persian characters json sent from android:

{"price":"5668845555","address":"بلاتذزییبغاتتاا","phoneNumber":"544585533","bedRoom":6,"describtion":"یباننترلبقثس","isForSale":"rent","isCommertial":"not commertial","meter":545,"image":"android.graphics.Bitmap@c10755a"}

while both json's is completely correct the json with persian characters dont inserts to table correctly!

this is my php file:

<?php

define("SERVER_NAME", "localhost");
define("USER_NAME", "root");
define("PASSWORD", "");
define("DATABASE_NAME", "mydb");

$conn = mysqli_connect(SERVER_NAME, USER_NAME, PASSWORD, DATABASE_NAME);


header("content-type: application/json;charset=utf-8");



$jsonContent = file_get_contents('php://input');
mysqli_set_charset($conn, "utf8");
utf8_encode($jsonContent);


$arrayContent = json_decode($jsonContent,true);


$price = $arrayContent["price"];
$address = $arrayContent["address"];
$phoneNumber=$arrayContent["phoneNumber"];
$image=$arrayContent["image"];
$meter=$arrayContent["meter"];
$bedRoom=$arrayContent["bedRoom"];
$isCommertial=$arrayContent["isCommertial"];
$isForSale=$arrayContent["isForSale"];

$describtion=$arrayContent["describtion"];




// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO estates (price,address,phoneNumber,image,meter,bedRoom,isCommertial,isForSale,describtion)
VALUES ('$price','$address','$phoneNumber','$image','$meter','$bedRoom','$isCommertial','$isForSale','$describtion')";

if ($conn->query($sql) === TRUE) {
    echo "Estate added successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();
?>

and this is what mysql table looks like after these two json's

see mysql table image

and i have to say that all of my fields collation is utf8_persian_ci

so what do you think is my mistake? thank you

Problem Solved! by using String Formatter Class in android i sent json in right way to php!

public class StringFormatter {

    // convert UTF-8 to internal Java String format
    public static String convertUTF8ToString(String s) {
        String out = null;
        try {
            out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            return null;
        }
        return out;
    }

    // convert internal Java String format to UTF-8
    public static String convertStringToUTF8(String s) {
        String out = null;
        try {
            out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
        } catch (java.io.UnsupportedEncodingException e) {
            return null;
        }
        return out;
    }

}
Salar Arabpour
  • 435
  • 2
  • 9
  • Please don’t concatenate SQL strings. It leads to lots of bugs and SQL injections. Use parameters. It may or may not be a problem here but it will be in the future. So the insert doesn’t return an error? – Sami Kuhmonen Mar 21 '19 at 10:53
  • Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Mar 21 '19 at 10:58
  • i log the result of php file and it only says Estate added successfully! – Salar Arabpour Mar 21 '19 at 11:02
  • JSON should already be UTF-8 or the persian chars would no show as such – RiggsFolly Mar 21 '19 at 11:05
  • You dont need thsi line `header("content-type: application/json;charset=utf-8");` – RiggsFolly Mar 21 '19 at 11:06
  • It makes more sense to check the connection `if ($conn->connect_error) {` right after you make the connection. – RiggsFolly Mar 21 '19 at 11:07
  • Sounds like there's a problem with the json you receive. Check the return value of json_last_error. If json was ok, at least the price should be inserted and it's not. – Joni Mar 21 '19 at 12:25

0 Answers0