2016-01-04 18 views
0

我有一個表信息作爲圖像下方如何從PHP返回值到java?

enter image description here

的ID設置爲自動遞增。當addInformation被調用時,它會將數據插入到信息中。

addInformation(name, weather, date2, status, first1[1], last1[1]); 

AddInformation功能

public void addInformation(final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut) { 
     class AddInfo extends AsyncTask<String, Void, String> { 
      ProgressDialog loading; 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait", null, true, true); 
      } 

      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       loading.dismiss(); 
       Toast.makeText(getApplicationContext(),s, Toast.LENGTH_LONG).show(); 
       //addWorkForce(Sub, NoP, NoH, Long.parseLong(s)); 
       // addWorkDetails(results, Long.parseLong(s)); 
      } 

      @Override 
      protected String doInBackground(String... params) { 

       HashMap<String, String> data = new HashMap<String, String>(); 
       data.put(Config.KEY_USER_NAME, name); 
       data.put(Config.KEY_WEATHER, weather); 
       data.put(Config.KEY_DATE, date2); 
       data.put(Config.KEY_STATUS, status); 
       data.put(Config.KEY_TIMEIN, timeIn); 
       data.put(Config.KEY_TIMEOUT, timeOut); 
       RequestHandler rh = new RequestHandler(); 
       String result = rh.sendPostRequest(Config.ADD_INFORMATION, data); 
       String response=? // get last Id from php 
       return result; 
      } 
     } 

     AddInfo ru = new AddInfo(); 
     ru.execute(name, weather, date2, status, timeIn, timeOut); 
    } 

我在PHP新的,現在想從表中信息獲取lastID和返回值到Android。但我得到未定義的索引。

<?php 

    class AddInformation{ 


     function response(){ 
      /** @var mysqli $con */ 
      require_once('dbConnect.php'); //$con = new mysqli('127.0.0.1', 'root', '', 'so'); 
      $name = $con->real_escape_string($_POST['name']); 
      $weather = $con->real_escape_string($_POST['weather']); 
      $date = $con->real_escape_string($_POST['date']); 
      $status = $con->real_escape_string($_POST['status']); 
      $timein = $con->real_escape_string($_POST['timeIn']); 
      $timeout = $con->real_escape_string($_POST['timeOut']); 
      $con->query("INSERT INTO information (name, weather, date, status, time_in, time_out) VALUES ('$name', '$weather', '$date', $status', '$timeIn', '$timeOut')"); 
      echo $con->insert_id; 
     } 
    } 

    $ai = new AddInformation(); 
    $ai->response(); 
?> 
  1. 如何最後的ID從PHP(插入LastId)返回響應(JAVA)/
  2. 如何在PHP解決不確定的指數?

有人可以幫我嗎?讚賞。

+0

你應該使用MySQLi的[準備語句](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)來查看你正在使用的MySQLi。 – Script47

+0

@ Script47它可以返回值爲java? –

回答

0

我從不努力工作開始,使用PDO並準備好語句而不是mysqli並自己清理數據。

自從我使用mysqli以來已經有一段時間了,但$ con> insert_id應該可以工作,除非出現錯誤。

插入是否工作?

這樣做。

if (!$con->query("INSERT INTO information (name, weather, date, status, time_in, time_out) VALUES ('$name', '$weather', '$date', $status', '$timeIn', '$timeOut')")) { 
    printf("Error message: %s\n", $con->error); 
    exit; 
} 
return $con->insert_id; 

然後在執行代碼中。

header('Content-Type: application/json');  
echo json_encode($ai->response()); 

現在,您正在通過Web服務將插入ID返回給Java。

然後在你的Java解碼JSON響應。

如果你在insert_id調用中得到一個未定義的值,那麼你的插入不能正常工作。