2015-04-18 234 views
2

我的問題是插入到數據庫中的值是像éèçàيبلا發送來自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'); 
?> 

太感謝你了,我花了幾天尋找一種方式來解決這個..

回答

0

漫長的探索後,我發現了許多成果......他們都不固定我的問題..但這些結果我得到了什麼可能會導致probleme想法..所以大家誰具有相同probleme做到這一點:

A/首先檢查你的數據庫(我的問題在這裏)。 在phpMyAdmin創建數據庫時選擇utf8_general_ci 見截圖:http://i62.tinypic.com/2ards7.png ,這裏是我的表結構:https://www.dropbox.com/s/d0hgdrbx14cbl85/rechmed.sql?dl=0

B/轉到Java源代碼,並添加這樣的:(對我來說)

而不是
entity.addPart("Msg", new StringBody(URLEncoder.encode("HERE IS THE STRING éçèà")); 

entity.addPart("Msg", new StringBody("HERE IS THE STRING éçèà")); 

,而不是發佈 「你的字符串」 後「URLEn coder.encode(MSG))」

C/現在去你的PHP代碼,並在數據庫中插入之前在我的情況你解碼URL編碼字符串形式 (): 現在我用這個:

$Msg = $_REQUEST['Msg']; 
$Msg= $db->real_escape_string(htmlentities(urldecode($Msg))); 

,而不是隻使用:

$Msg = $_REQUEST['Msg']; 

這是我的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(URLEncoder.encode(msg))); 
       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 = $_REQUEST['Msg']; 
    $Mdp = $_REQUEST['Mdp']; 
    $Msg= $db->real_escape_string(htmlentities(urldecode($Msg)));  
    $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'); 
mysqli_query($db,"SET NAMES 'utf8'"); 
mysqli_query($db,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'"); 

?> 
0

在PHP代碼中,嘗試使用utf8_decode($ _ REQUEST [「request_value」])每次嘗試檢索時間即將到來的價值。

+0

它並不能幫助..我覺得問題是在javacode –

+0

好吧,mybe ....在一個開發中,我有同樣的問題...「utf8_decode」幫助我解決這個問題。如果你嘗試「utf8_decode」,並不能幫助你解決你的問題..所以....對不起。目前沒有更多的解決方案。 –

+0

看到我的答案@ToukeaTatsi –