Skip to content
Advertisement

Android passing username to php

I am building an android app where the users are expected to perform certain tasks individually assigned to them. To download the project information I am using HTTPServiceClass and passing in a url in my android app. The code is as follows

String getProjectsURL = "http://192.168.0.102/syncsqlite/getProjects.php";

Inside doInBackground I have the following

protected Void doInBackground(Void... arg0) {
            HttpServiceClass httpServiceClass = new HttpServiceClass(getProjectsURL);

            try {
                httpServiceClass.ExecutePostRequest();


                if (httpServiceClass.getResponseCode() == 200) {
                    FinalJSonResult = httpServiceClass.getResponse();
                    if (FinalJSonResult != null) {
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = new JSONArray(FinalJSonResult);
                            JSONObject jsonObject;
                            for (int i = 0; i < jsonArray.length(); i++) {
                                jsonObject = jsonArray.getJSONObject(i);
                                String projectName = jsonObject.getString("ProjectName");
                                String projectDescription = jsonObject.getString("ProjectDescription");
                                String projType = jsonObject.getString("projType");
                                String projStatus = jsonObject.getString("projectStatus");
                                String SQLiteDataBaseQueryHolder = "INSERT INTO "+SQLiteHelper.TABLE_NAME+" (ProjectName,ProjectDescription,projType,projectStatus) VALUES('"+projectName+"', '"+projectDescription+"', '"+projType+"', '"+projStatus+"');";
                                sqLiteDatabase.execSQL(SQLiteDataBaseQueryHolder);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                } else {
                    Toast.makeText(context, httpServiceClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

I am using PHP and MYSQL. The php code is as follows:

$sql = "SELECT * FROM projects where projectStatus=1";

$result = $conn->query($sql);

if ($result->num_rows >0) {


 while($row[] = $result->fetch_assoc()) {
 $tem = $row;
 $json = json_encode($tem);
 }

} else {
 echo "No Results Found.";
}

This is working without any problem. However, I want to filter out the project information to specific users by adding a where condition in my select query. How do I pass the app user login information through my getProjects.php. Can I add it as an argument to the getProjectsURL?

Advertisement

Answer

First send user ID to the page as URL parameter as given below

String UserId  = user.ID; //This is a demo code
String getProjectsURL = "http://192.168.0.102/syncsqlite/getProjects.php?userId="+UserId;

In your PHP code you can get the User ID as given below

...
$userID  = (isset($_GET['userId']))?  $_GET['userId'] : 0;
$sql = "SELECT * FROM projects where projectStatus = 1 AND user_id = '".$userID."'";
...
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement