简体   繁体   中英

taking a symbol from edittext and save it in database

I'm trying to take a symbol from an EditText in my Android application and save it in the database (it's retrieved from the database and I'm editing it). For example, I have this symbol: μ . It will be retrived fine, but when I edit it and save the changes the symbol will be saved as a ? , and I don't know why.

I've added this to my PHP file:

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

and this is the Java code

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

my php code

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

the db_connect

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

}

?>

How can I solve this, and where is the problem?

Alright, I think I've got your problem.

Before every insert query on the server side, try adding

mysql_set_charset('utf8',$link);

This is because mysql_query("SET NAMES 'utf8'"); tells the database what charset to expect, but the mysql is not sending it with that charset. Let me know if this works.

This may not be the problem, but if you execute mysql_client_encoding($link); on the php script and it does not return utf8 then this is most likely the issue.

Also, you should know that mysql_* functions are deprecated. You should use mysqli_ or PDO .

Source

Ok now that I've seen more code, I see you're doing alot of things wrong.

Here's a pastie you can use to see the changes I've made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM