简体   繁体   中英

Insert special Characters / PHP + MySql + Android

This question has been asked a lot of times but none of suggested solutions worked for me :(

I need to insert special characters, as ' (closing apostrophe) for example, into MySql DB via PHP using Android.

MySql Table is defined as:

ENGINE=InnoDB DEFAULT CHARSET=utf8;

Data is passed to PHP, via Java , so:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1", value1));       
nameValuePairs.add(new BasicNameValuePair("param2", value2));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("blabla.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
response = httpclient.execute(httppost);

PHP receives it so:

//To be able to receive UTF-8 (needed?)


mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');

$param1 = $_POST['param1'];
$param2 = $_POST['param2'];

//Convertsion to UTF-8 (needed?)
$param1 = utf8_decode($param1);
$param2 = utf8_decode($param2);

$mysqli = new mysqli($hostname, $username, $password, $dbname);
mysqli_set_charset('utf8',$mysqli);
$mysqli->prepare("INSERT INTO ...

Insert command inserts the record, but special characters becomes "?" in the table. Any help?

ie "A'B" becomes in the DB: "A?B"

Thank you

删除对utf8_decode函数的调用就可以了!

Try mysqli_real_escape_string() function. This escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection.

Example:

mysqli_real_ecape_string($mysqli,$param1);
mysqli_real_ecape_string($mysqli,$param2);

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