Search code examples
phpandroidmysqlreturntoast

Value always return 0 where it should return number of row inserted


I have two table, one is Information and the other is WorkForce

Information

enter image description here

WorkForce

enter image description here

The twf column in WorkForce is used to get the id of Information, which suppose return as 1, but it return value 0. If the id in Information is 5, twf should be 5 too.

Firstly I use the a to represent the number of selected row and then add the parameter of a into addWorkForce. The toast always display 0 .

  a=addInformation(name,weather,date2,status,first1[1],last1[1]);
  Toast.makeText(getApplicationContext(),a+"",Toast.LENGTH_LONG).show();
  addWorkForce(Sub, NoP, NoH,a);

addInformation function

  public long 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();
            }

            @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);
                return  result;
            }
        }

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

        return 0;

    }

Can someone help me to figure out the problem ?

addInformation.php

<?php 

    if($_SERVER['REQUEST_METHOD']=='POST'){

        //Getting values
        $name = $_POST['name'];
        $weather = $_POST['weather'];
        $date = $_POST['date'];
                $status = $_POST['status'];
                $timeIn = $_POST['timeIn'];
                $timeOut = $_POST['timeOut'];

        //Creating an sql query
        $sql = "INSERT INTO information(name, weather, date, status, time_in, time_out) VALUES ('$name','$weather','$date', '$status', '$timeIn', '$timeOut')";

        //Importing our db connection script
        require_once('dbConnect.php');

        //Executing query to database
        if(mysqli_query($con,$sql)){
            echo 'Information Added Successfully';
        }else{
            echo 'Could Not Add Information';
        }

        //Closing the database 
        mysqli_close($con);
    }
?>

This is how I used the code to get the row inserted into sqlite and it works

 a = ts.insertTimeSheet(name, weather, date2, status, first1[1], last1[1]);

TimeSheet

 public long insertTimeSheet(String name, String weather, String date, String status, String TimeIn, String TimeOut) {
        database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(MyDatabaseHelper.Name, name);
        values.put(MyDatabaseHelper.Weather, weather);
        values.put(MyDatabaseHelper.Date, date);
        values.put(MyDatabaseHelper.Status, status);
        values.put(MyDatabaseHelper.TimeIn_Info, TimeIn);
        values.put(MyDatabaseHelper.TimeOut_Info, TimeOut);
        return database.insert(MyDatabaseHelper.TABLE_INFO, null, values); // if the id in Information is 5, twf display 5 too

Solution

  • As per your code to add information:

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

    Here, you are calling addInformation on main thread and hold the return value to variable a. But the problem is, your actual implementation of adding information is done in doInBackground of AsyncTask which actually does the job on a worker thread (async call) and so you will always get the default value for a=0 (as a is long).

    What you need to do is to move your code to onPostExecute function of AsyncTask like below:

    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,a);
                }
    
                @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);
                    return  result;
                }
            }
    
             AddInfo ru = new AddInfo();
             ru.execute(name,weather,date2,status,timeIn,timeOut);
    
        }
    

    There is another approach to use callbacks. Implement an interface and after completion of your AsyncTask, you can send callback to do another job.