2017-01-17 4 views
0

내 안드로이드 프로그램이 작동하지만 데이터베이스에서 정보를 삭제하거나 정보를 얻으려고해도 아무런 효과가 없습니다. 그것은 단지 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); 

?> 

$_GET과 사람은 어떻게됩니까 그리고 이것은 코딩 안드로이드입니다

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

? 브라우저의 'localhost server PC'와 장치에서 URL이 작동합니까? –

+0

그런 다음 $ _GET 대신 $ _REQUEST를 사용하십시오. $ _REQUEST는 두 가지 종류의 요청에 모두 적용됩니다. – samsad

+0

삭제 단추를 누르면 확인 부분이 나타나지만 '예'를 누르면 ViewList.class로 돌아가지만 데이터베이스에서는 삭제되지 않습니다. URL에 Deleted Successful이 표시되므로 – 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의 경우 내 안드로이드 에뮬레이터에는 아무 것도 표시되지 않지만 localhost URL을 입력하면 [null] . deleteLogin.php의 경우 URL에 Deleted가 성공적으로 표시되지만 안드로이드 에뮬레이터에는 아무 것도 표시되지 않습니다. – Leonard

+0

'print_r ($ result);의 결과는 무엇입니까? –

관련 문제