我的問題是插入到數據庫中的值是像éèçà
或يبلا
發送來自Android的特殊字符(字符串)到PHP
這裏問號???????
發送時字符串我的Java代碼
private void uploadFile(String filePath) {
try {
try {
HttpPost httpost = new HttpPost(url_upload);
MultipartEntity entity = new MultipartEntity();
entity.addPart("Mdp", new StringBody(mdp));
entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà"));
entity.addPart("type", new StringBody(type));
if(type.equals("1")) {
entity.addPart("Emailm", new StringBody(email));
entity.addPart("Emailp", new StringBody(emaildest));
}else {
entity.addPart("Emailp", new StringBody(email));
entity.addPart("Emailm", new StringBody(emaildest));
}
if (attach){
entity.addPart("attachement", new FileBody(new File(filePath)));
entity.addPart("attacher", new StringBody("oui"));
}else{
entity.addPart("attacher", new StringBody("non"));
}
httpost.setEntity(entity);
HttpResponse response;
HttpClient httpclient = new DefaultHttpClient();
response = httpclient.execute(httpost);
if (response != null) {
HttpEntity entity2 = response.getEntity();
String responseString = EntityUtils.toString(entity2, "UTF-8");
System.out.println(responseString);
json2=new JSONObject(responseString);
} else { //erreur
}
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e){
}
}
PHP代碼
<?php
date_default_timezone_set ("Africa/Algiers");
include('connect.php');
$response = array();
if (isset($_REQUEST['Emailm'])&& isset($_REQUEST['Mdp'])&&isset($_REQUEST['type']) && isset($_REQUEST['Emailp'])&&isset($_REQUEST['Msg']) ) {
$Emailm = $db->real_escape_string($_REQUEST['Emailm']);
$Emailp = $db->real_escape_string($_REQUEST['Emailp']);
$Mdp = $db->real_escape_string($_REQUEST['Mdp']);
$Msg=$db->real_escape_string($_REQUEST['Msg']);
$Mdp = $_REQUEST['Mdp'];
$type= $_REQUEST['type'];
$target_path1 = "attachement/";
if($type=="0"){ //compte patient
if ($result = $db->query("SELECT * FROM `patient` WHERE `Email_p`='$Emailp' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}else { //compte médecin
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
$row_cnt = $result->num_rows; }
}
$b=false;
$a=false;
if ($row_cnt>0) {
if($_REQUEST['attacher']=="oui"){
$filecount = 0;
$files = glob($target_path1 . "*");
if ($files){
$filecount = count($files);
}
$nomFichier=$filecount.basename($_FILES['attachement']['name']) . ".a";
$target_path1 = $target_path1 . $nomFichier;
$nomFichierOrigin=basename($_FILES['attachement']['name']);
if(move_uploaded_file($_FILES['attachement']['tmp_name'], $target_path1)) {
$b=true;}
}
else{
$nomFichier="";
$nomFichierOrigin="";
};
$h=new DateTime("now") ;
$s=$h->format('G:i:s');
$s2=$h->format('Y-m-d');
if($result = mysqli_query($db,"INSERT INTO `msg`(`cle`, `email_m`, `email_p`, `message`, `attachement`, `type`, `attachementNomOrigin`, `heure`, `date`,`vu`)
VALUES ('','$Emailm','$Emailp','$Msg','$nomFichier','$type','$nomFichierOrigin','$s','$s2','non')"))
{$a=true;}
if($a==true && $b==true && $_REQUEST['attacher']=="oui"){
$dernierId = $db->insert_id;
if ($result = $db->query("SELECT * FROM `med` WHERE `Email`='$Emailm' AND `Mdp`='$Mdp'")) {
if (mysqli_num_rows($result) > 0) {
$response["Msg"] = array();
while ($row = mysqli_fetch_array($result)) {
$Msg = array();
$Msg["cle"] = intval($row["cle"]);
$Msg["heure"] = $row["heure"];
$Msg["date"] = $row["date"];
$Msg["email_m"] = $row["email_m"];
$Msg["email_p"] = $row["email_p"];
$Msg["message"] = $row["message"];
$Msg["type"] = $row["type"];
$Msg["attachement"] = $row["attachement"];
$Msg["attachementNomOrigin"] = $row["attachementNomOrigin"];
array_push($response["Msg"], $Msg);
}
$response["success"] = 1;
$response["message"] = "Succès";
}}} else
if($a==true && $_REQUEST['attacher']=="non"){
$response["success"] = 1;
$response["message"] = "Succès";
}else{
$response["success"] = 0;
$response["message"] = "Erreur lors de l'envoi";
}
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Email ou Mot de passe incorrect";
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "Champs manqués";
echo json_encode($response);
}
?>
這裏的connect.php
<?php
$db = new mysqli('localhost', 'root', '', 'rechmed');
mysqli_set_charset($db,'utf8');
?>
太感謝你了,我花了幾天尋找一種方式來解決這個..
它並不能幫助..我覺得問題是在javacode –
好吧,mybe ....在一個開發中,我有同樣的問題...「utf8_decode」幫助我解決這個問題。如果你嘗試「utf8_decode」,並不能幫助你解決你的問題..所以....對不起。目前沒有更多的解決方案。 –
看到我的答案@ToukeaTatsi –