Php 从edittext中获取符号并将其保存在数据库中

Php 从edittext中获取符号并将其保存在数据库中,php,android,Php,Android,我试图从Android应用程序中的EditText中获取一个符号,并将其保存在数据库中,然后从数据库中检索并编辑它。例如,我有这个符号:μ。它会被很好地检索,但是当我编辑它并保存更改时,符号将被保存为?,我不知道为什么 我已将此添加到我的PHP文件中: mysql_query("SET NAMES 'utf8'"); header("Content-type: application/json; charset=utf-8"); 这是Java代码 class SaveglossaryDetai

我试图从Android应用程序中的EditText中获取一个符号,并将其保存在数据库中,然后从数据库中检索并编辑它。例如,我有这个符号:μ。它会被很好地检索,但是当我编辑它并保存更改时,符号将被保存为?,我不知道为什么

我已将此添加到我的PHP文件中:

mysql_query("SET NAMES 'utf8'");
header("Content-type: application/json; charset=utf-8");
这是Java代码

class SaveglossaryDetails extends AsyncTask<String, String, String> {


/**
 * Saving glossary
 * */
protected String doInBackground(String... args) {

    // getting updated data from EditTexts
    String Name = name.getText().toString();
    String Description = description.getText().toString();
    String Symbol = symbol.getText().toString();

    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(TAG_GID, gid));
    params.add(new BasicNameValuePair(TAG_NAME, Name));
    params.add(new BasicNameValuePair(TAG_DESCRIPTION, Description));
    params.add(new BasicNameValuePair(TAG_SYMBOL, Symbol));

    // sending modified data through http request
    // Notice that update glossary url accepts POST method
    JSONObject json = jsonParser.makeHttpRequest(url_update_glossary,"POST", params);

    // check json success tag
    try {
        int success = json.getInt(TAG_SUCCESS);

        if (success == 1) {
            // successfully updated
            Intent i = new Intent(getApplicationContext(), AdminGlossary.class);
            i.putExtra("admin", admin);
            startActivity(i);
        } else {
            // failed to update glossary
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}
我的php代码

<?php


// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['ID_glossary']) && isset($_POST['symbol']) && isset($_POST['name'])  && isset($_POST['description'])) {

$ID_glossary = $_POST['ID_glossary'];
$name = $_POST['name'];
$symbol = $_POST['symbol'];
$description = $_POST['description'];


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();
mysql_query("SET NAMES 'utf8'");

// mysql update row with matched pid
$result = mysql_query("UPDATE glossary SET Symbol='$symbol', Name = '$name',     Description = '$description' WHERE ID_Glossary = $ID_glossary");
header("Content-type: application/json; charset=utf-8");

// check if row inserted or not
if ($result) {
    // successfully updated
    $response["success"] = 1;
    $response["message"] = "glossary successfully updated.";

    // echoing JSON response
    echo json_encode($response);
} else {

}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>
db_连接

<?php

/**
 * A class file to connect to database
 */
class DB_CONNECT {

// constructor
function __construct() {
    // connecting to database
    $this->connect();
}

// destructor
function __destruct() {
    // closing db connection
    $this->close();
}

/**
 * Function to connect with database
 */
function connect() {
    // import database connection variables
    require_once __DIR__ . '/db_config.php';

    // Connecting to mysql database
    $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

    // Selecing database
    $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
mysqli_query($link, "SET SESSION CHARACTER_SET_RESULTS =utf8;");
mysqli_query($link, "SET SESSION CHARACTER_SET_CLIENT =utf8;");
    // returing connection cursor
    return $con;
}

/**
 * Function to close db connection
 */
function close() {
    // closing db connection
    mysql_close();
}

}

?>

我该如何解决这个问题?问题在哪里?

好吧,我想我已经解决了你的问题

在服务器端的每个插入查询之前,请尝试添加

mysql_set_charset('utf8',$link);
这是因为mysql_querySET命名为“utf8”;告诉数据库所需的字符集,但mysql不会将其与该字符集一起发送。让我知道这是否有效

这可能不是问题,但如果执行mysql\u client\u encoding$link;在php脚本上,如果它不返回utf8,那么这很可能就是问题所在

另外,您应该知道mysql_*函数已被弃用。您应该使用mysqli_uu或PDO


好的,现在我已经看到了更多的代码,我看到您做了很多错事


这是一个可以用来查看我所做更改的示例

您可以尝试记录字符串以查看Java是否没有正确捕获值吗?@JayS。问题是我不能在虚拟机上运行应用程序来记录它,因为它太大了,我只能在真实的设备上运行。啊,如果你有Android SDK,并在设备上启用USB调试,你可以在计算机上使用DDMS来查看控制台@lama@JayS. 我做了并记录了字符串,并且正确记录了值!有什么问题吗进展如何@喇嘛,你解决问题了吗?试过了,但也没用。。如何使用mysql\u client\u encoding$link;在我的代码里?我只是照样复制粘贴?因为我复制了,什么也没有返回。我发布了完整的php文件。你也可以发布db_connect类吗?因为链接应该在里面。如果我们知道这一点,我们还可以修复mysql\u set\u字符集。我已经编辑了我的答案@lamai发布了它,它被设置为latin1,然后我将它们改为utf8,但这并没有解决问题:你是如何改变它的@我的天啊。我不知道。也许尝试删除问题并重新发布?也许其他人可以帮你@好的,我会。。真的非常感谢你的努力,感谢你没有忽视我!!是的,没问题,喇嘛