$ _GET的PHP文件不起作用。 Android编码没有错误,但没有工作

时间:2017-01-17 04:55:52

标签: java php android android-studio

我的android程序有效,但当我尝试删除或从我的数据库中获取信息时,它没有任何工作。它只发生在PHP文件中带有$_GET的那些

<?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";
}

1 个答案:

答案 0 :(得分: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);

?>