使用波斯字符导致mysql表的每个字段插入值null

时间:2019-03-21 10:51:17

标签: php android mysql

我是一名新的Android开发人员,正在开发波斯房地产android项目。我有一种使用php文件将新房地产插入到mysqli表中的表格。但是当我在该表格的每个字段中使用波斯字符时,它实际上会向其中插入空值table。的每个字段,但是当我使用英文字符时,它将完全插入到表格中! 例如,这是我从android发送的英文字符json:

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

,其中包含从android发送的波斯字符json:

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

虽然两个json都完全正确,但波斯字符的json不能正确插入表中!

这是我的php文件:

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

这是这两个json之后的mysql表的样子

see mysql table image

我不得不说,我所有的字段排序规则都是utf8_persian_ci

那么您认为我的错误是什么? 谢谢

问题已解决! 通过在Android中使用String Formatter类,我以正确的方式向php发送了json!

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;
    }

}

0 个答案:

没有答案