2017-01-17 38 views
0

我的android程序的工作原理,但當我嘗試刪除或從我的數據庫中獲取信息它不起作用。它只是發生在那些與$_GET在PHP文件

<?php 

deleteLogin.php 
//Getting Id 
$id = $_GET['id']; 

//Importing database 
require_once('dbConnect.php'); 

//Creating sql query 
$sql = "DELETE FROM database_data WHERE id='$id';"; 

//Deleting record in database 
if(mysqli_query($con,$sql)){ 
echo 'Deleted Successfully'; 
}else{ 
echo 'Could Not Delete Try Again'; 
} 

//closing connection 
mysqli_close($con); 

?> 


<?php 

getLogin.php 
//Getting the requested id 
$id = $_GET["id"]; 

//Importing database 
require_once('dbConnect.php'); 

//Creating sql query with where clause to get an specific employee 
$sql = "SELECT * FROM database_data WHERE id='$id';"; 

//getting result 
$r = mysqli_query($con,$sql); 

//pushing result to an array 
$result = array(); 
$row = mysqli_fetch_array($r); 
array_push($result,array(
"id"=>$row['id'], 
"username"=>$row['username'], 
"password"=>$row['password'], 
"email"=>$row['email'] 
)); 

//displaying in json format 
echo json_encode(array('result'=>$result)); 

mysqli_close($con); 

?> 

這是Android編碼

package com.kopitiam.waiterapplication; 

import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.HashMap; 

public class ViewAccounts extends AppCompatActivity implements View.OnClickListener { 

private EditText editTextId; 
private EditText editTextUsername; 
private EditText editTextPassword; 
private EditText editTextEmail; 

private Button buttonUpdate; 
private Button buttonDelete; 

private String id; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view_accounts); 

    Intent intent = getIntent(); 

    id = intent.getStringExtra(ConfigAdmin.EMP_ID); 

    editTextId = (EditText) findViewById(R.id.editTextId); 
    editTextUsername = (EditText) findViewById(R.id.editTextUsername); 
    editTextPassword = (EditText) findViewById(R.id.editTextPassword); 
    editTextEmail = (EditText) findViewById(R.id.editTextEmail); 

    buttonUpdate = (Button) findViewById(R.id.buttonUpdate); 
    buttonDelete = (Button) findViewById(R.id.buttonDelete); 

    buttonUpdate.setOnClickListener(this); 
    buttonDelete.setOnClickListener(this); 

    editTextId.setText(id); 

    getEmployee(); 
} 

private void getEmployee(){ 
    class GetEmployee extends AsyncTask<Void,Void,String>{ 
     ProgressDialog loading; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      loading = ProgressDialog.show(ViewAccounts.this,"Fetching...","Wait...",false,false); 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      loading.dismiss(); 
      showEmployee(s); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      BackgroundWorkerAdmin rh = new BackgroundWorkerAdmin(); 
      String s = rh.sendGetRequestParam(ConfigAdmin.getlogin_url,id); 
      return s; 
     } 
    } 
    GetEmployee ge = new GetEmployee(); 
    ge.execute(); 
} 

private void showEmployee(String json){ 
    try { 
     JSONObject jsonObject = new JSONObject(json); 
     JSONArray result = jsonObject.getJSONArray(ConfigAdmin.TAG_JSON_ARRAY); 
     JSONObject c = result.getJSONObject(0); 
     String username = c.getString(ConfigAdmin.TAG_USERNAME); 
     String password = c.getString(ConfigAdmin.TAG_PASSWORD); 
     String email = c.getString(ConfigAdmin.TAG_EMAIL); 

     editTextUsername.setText(username); 
     editTextPassword.setText(password); 
     editTextEmail.setText(email); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 


private void updateEmployee(){ 
    final String username = editTextUsername.getText().toString().trim(); 
    final String password = editTextPassword.getText().toString().trim(); 
    final String email = editTextEmail.getText().toString().trim(); 

    class UpdateEmployee extends AsyncTask<Void,Void,String>{ 
     ProgressDialog loading; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      loading = ProgressDialog.show(ViewAccounts.this,"Updating...","Wait...",false,false); 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      loading.dismiss(); 
      Toast.makeText(ViewAccounts.this,s,Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      HashMap<String,String> hashMap = new HashMap<>(); 
      hashMap.put(ConfigAdmin.KEY_ID,id); 
      hashMap.put(ConfigAdmin.KEY_USERNAME,username); 
      hashMap.put(ConfigAdmin.KEY_PASSWORD,password); 
      hashMap.put(ConfigAdmin.KEY_EMAIL,email); 

      BackgroundWorkerAdmin rh = new BackgroundWorkerAdmin(); 

      String s = rh.sendPostRequest(ConfigAdmin.updatelogin_url,hashMap); 

      return s; 
     } 
    } 

    UpdateEmployee ue = new UpdateEmployee(); 
    ue.execute(); 
} 

private void deleteEmployee(){ 
    class DeleteEmployee extends AsyncTask<Void,Void,String> { 
     ProgressDialog loading; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      loading = ProgressDialog.show(ViewAccounts.this, "Updating...","Wait...",false,false); 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      loading.dismiss(); 
      Toast.makeText(ViewAccounts.this, s, Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      BackgroundWorkerAdmin rh = new BackgroundWorkerAdmin(); 
      String s = rh.sendGetRequestParam(ConfigAdmin.deletelogin_url, id); 
      return s; 
     } 
    } 

    DeleteEmployee de = new DeleteEmployee(); 
    de.execute(); 
} 

private void confirmDeleteEmployee(){ 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    alertDialogBuilder.setMessage("Are you sure you want to delete this employee?"); 

    alertDialogBuilder.setPositiveButton("Yes", 
      new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
        deleteEmployee(); 
        startActivity(new Intent(ViewAccounts.this,ViewList.class)); 
       } 
      }); 

    alertDialogBuilder.setNegativeButton("No", 
      new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 

       } 
      }); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 
} 

@Override 
public void onClick(View v) { 
    if(v == buttonUpdate){ 
     updateEmployee(); 
    } 

    if(v == buttonDelete){ 
     confirmDeleteEmployee(); 
    } 
} 
} 

ConfigAdmin文件

package com.kopitiam.waiterapplication; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class ConfigAdmin { 
    //Address of our scripts of the CRUD 
    public static final String register_url = "http://172.22.63.195/register.php"; 
    public static final String getalllogin_url = "http://172.22.63.195/getallLogin.php"; 
    public static final String getlogin_url = "http://172.22.63.195/getLogin.php"; 
    public static final String deletelogin_url = "http://172.22.63.195/deleteLogin.php"; 
    public static final String updatelogin_url = "http://172.22.63.195/updateLogin.php"; 

    //Keys that will be used to send the request to php scripts 
    public static final String KEY_ID = "id"; 
    public static final String KEY_USERNAME = "username"; 
    public static final String KEY_PASSWORD = "password"; 
    public static final String KEY_EMAIL = "email"; 

    //JSON Tags 
    public static final String TAG_JSON_ARRAY="result"; 
    public static final String TAG_ID = "id"; 
    public static final String TAG_USERNAME = "username"; 
    public static final String TAG_PASSWORD = "password"; 
    public static final String TAG_EMAIL = "email"; 

    //employee id to pass with intent 
    public static final String EMP_ID = "emp_id"; 
} 
+0

你得到什麼錯誤?該網址是否在您的瀏覽器中運行 - 您的「本地主機服務器PC」和您的設備上? –

+0

然後使用$ _REQUEST而不是$ _GET。 $ _REQUEST適用於這兩種請求GET和POST – samsad

+0

按下刪除按鈕後,確認部分出來,但是當我按'是'時,它只是回到ViewList.class,但不會從數據庫中刪除。在URL顯示刪除成功,所以它的工作原理 – Leonard

回答

0

嘗試這樣......是不是需要分號在單個查詢的最後。當綁定到數組時,使用while循環。 deleteLogin.php

<?php 
//Getting Id 
$id = $_REQUEST['id']; 

//Importing database 
require_once('dbConnect.php'); 

//Creating sql query 
$sql = "DELETE FROM database_data WHERE id='$id'"; 

//Deleting record in database 
if(mysqli_query($con,$sql)){ 
echo 'Deleted Successfully'; 
}else{ 
echo 'Could Not Delete Try Again'; 
} 

//closing connection 
mysqli_close($con); 

?> 

getLogin.php

<?php 

//Getting the requested id 
$id = $_REQUEST["id"]; 

//Importing database 
require_once('dbConnect.php'); 

//Creating sql query with where clause to get an specific employee 
$sql = "SELECT * FROM database_data WHERE id='$id'"; 

//getting result 
$r = mysqli_query($con,$sql); 

//pushing result to an array 
//$result = array(); 
while ($row = mysqli_fetch_array($r)) { 
    $data = array(
    "id"=>$row['id'], 
    "username"=>$row['username'], 
    "password"=>$row['password'], 
    "email"=>$row['email'] 
); 
    $result[] = $data; 
} 

//print_r($result); 
//displaying in json format 
echo json_encode($result); 

mysqli_close($con); 

?> 
+0

對於getLogin.php我還是什麼也沒得到我的Android模擬器,但是當我在URL輸入localhost它表明[空] 。對於deleteLogin.php這表明已成功刪除URL中,但沒有出現在Android模擬器 – Leonard

+0

什麼的print_r($結果)的'輸出;' –

相關問題