I'd like you to help me understand something about my code.

I am creating an android app in which there is a Login and Registration form.

I need to edit PHP files by adding PDOs.

I was able to get the PHP file to work with PDO, but there is a problem.

When the user registers, his name and surname are saved in the SharedPreferences and each time he accesses the monster.

The problem is that since I modified the PHP file by adding PDO, if I access for example with user1, it shows me the first and last name of user2, and if I log in with user2, user3, user4 always shows me the name and surname of user2 .

Why?

With the PHP file without PDO it shows me the name and surname of the user with whom I log in.

Can anyone explain why this happens?

Login.php without PDO

<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){
  // echo $_SERVER["DOCUMENT_ROOT"];  // /home1/demonuts/public_html
//including the database connection file
       include_once("config2.php");

    $idAKr = $_POST['idAKr'];
    $cell = $_POST['cellulare'];

     if( $idAKr == '' || $cell == '' ){
            echo json_encode(array( "statusr" => "false","message" => "Inserisci numero di telefono!") );
     }else{
        $query= "SELECT * FROM Ristoratori WHERE cellulare='$cell'";
            $result= mysqli_query($con, $query);

            if(mysqli_num_rows($result) > 0){  
             $query= "SELECT * FROM Ristoratori WHERE cellulare='$cell'";
                         $result= mysqli_query($con, $query);
                     $emparray = array();
                         if(mysqli_num_rows($result) > 0){  
                         while ($row = mysqli_fetch_assoc($result)) {
                                     $emparray[] = $row;
                                   }
                         }
               echo json_encode(array( "statusr" => "truer","message" => "Accesso eseguito", "datar" => $emparray) );
            }else{ 
                echo json_encode(array( "statusr" => "false","message" => "Numero di telefono sbagliato!") );
            }
             mysqli_close($con);
     }
    } else{
            echo json_encode(array( "statusr" => "false","message" => "Errore, riprova!") );
    }
?>

Login.php with PDO

<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){
  // echo $_SERVER["DOCUMENT_ROOT"];  // /home1/demonuts/public_html
//including the database connection file

require_once('db.php');

    $idAKr = $_POST['idAKr'];
    $cell = $_POST['cellulare'];

     if( $idAKr == '' || $cell == '' ){
            echo json_encode(array( "statusr" => "false","message" => "Inserisci numero di telefono!") );
     }else{
         $conn=$dbh->prepare("SELECT cellulare FROM Ristoratori WHERE cellulare=?");
         $conn->bindParam(1,$cell);
         $conn->execute();

         if($conn->rowCount() !==0){

         $conn2=$dbh->prepare("SELECT COUNT(cellulare) FROM Ristoratori WHERE cellulare=?");
         $conn2->bindParam(1,$cell);
         $conn2->execute();
         $data_exists = ($conn2->fetchColumn());
         $emparray = array();

         if($data_exists > 0){

             $emparray = $conn->fetchAll(PDO::FETCH_ASSOC);


         }
         echo json_encode(array( "statusr" => "truer","message" => "Accesso eseguito", "datar" => $emparray) );

        }  else{ 
                echo json_encode(array( "statusr" => "false","message" => "Numero di telefono sbagliato!") );
            }  
     }
     $dbh = null;
    } else{
            echo json_encode(array( "statusr" => "false","message" => "Errore, riprova!") );
    }
?>

ParseContent.java

void saveInfoRistoratore(String responseRistoratore) {
        preferenceHelperRistoratore.putIsLoginRistoratore(true);
        try {
            JSONObject jsonObjectRistoratore = new JSONObject(responseRistoratore);
            if (jsonObjectRistoratore.getString(KEY_SUCCESSRistoratore).equals("truer")) {
                JSONArray dataArrayRistoratore = jsonObjectRistoratore.getJSONArray("datar");
                for (int i = 0; i < dataArrayRistoratore.length(); i++) {

                    JSONObject dataobjRistoratore = dataArrayRistoratore.getJSONObject(i);
                    preferenceHelperRistoratore.putNomeRistoratore(dataobjRistoratore.getString(AndyConstantsRistoratore.ParamsRistoratore.NOMERistoratore));
                    preferenceHelperRistoratore.putCognomeRistoratore(dataobjRistoratore.getString(AndyConstantsRistoratore.ParamsRistoratore.COGNOMERistoratore));
                    preferenceHelperRistoratore.putNomeRistorante(dataobjRistoratore.getString(AndyConstantsRistoratore.ParamsRistoratore.RISTORANTEmono));
                }
            }
        } catch (JSONException eRistoratore) {
            eRistoratore.printStackTrace();
        }

    }

PreferenceHelperRistoratore

public class PreferenceHelperRistoratore {

    private final String INTRORistoratore = "intro";
    private final String NOMERistoratore = "nome";
    private final String COGNOMERistoratore = "cognome";
    private final String RISTORANTEmono = "ristorante";


    private SharedPreferences app_prefsRistoratore;
    private Context contextRistoratore;

    PreferenceHelperRistoratore(Context contextRistoratore) {
        app_prefsRistoratore = contextRistoratore.getSharedPreferences("sharedr",
                Context.MODE_PRIVATE);
        this.contextRistoratore = contextRistoratore;
    }

    void putIsLoginRistoratore(boolean loginoroutRistoratore) {
        SharedPreferences.Editor editRistoratore = app_prefsRistoratore.edit();
        editRistoratore.putBoolean(INTRORistoratore, loginoroutRistoratore);
        editRistoratore.apply();
    }
    boolean getIsLoginRistoratore() {
        return app_prefsRistoratore.getBoolean(INTRORistoratore, false);
    }

    void putNomeRistoratore(String loginoroutRistoratore) {
        SharedPreferences.Editor editRistoratore = app_prefsRistoratore.edit();
        editRistoratore.putString(NOMERistoratore, loginoroutRistoratore);
        editRistoratore.apply();
    }
    public String getNomeRistoratore() {
        return app_prefsRistoratore.getString(NOMERistoratore, "");
    }

    void putCognomeRistoratore(String loginoroutRistoratore) {
        SharedPreferences.Editor editRistoratore = app_prefsRistoratore.edit();
        editRistoratore.putString(COGNOMERistoratore, loginoroutRistoratore);
        editRistoratore.apply();
    }
    public String getCognomeRistoratore() {
        return app_prefsRistoratore.getString(COGNOMERistoratore, "");
    }

    void putNomeRistorante(String loginroutRistoratore){
        SharedPreferences.Editor editRistoratore = app_prefsRistoratore.edit();
        editRistoratore.putString(RISTORANTEmono, loginroutRistoratore);
        editRistoratore.apply();
    }
    public String getNOMERistorante (){
        return app_prefsRistoratore.getString(RISTORANTEmono,"");
    }

}

Login.java

//LOGIN
    @SuppressLint("StaticFieldLeak")
    private void loginRistoratore() throws IOException, JSONException {

        if (!AndyUtilsRistoratore.isNetworkAvailableRistoratore(r_start.this)) {
            Toast.makeText(r_start.this, "Internet is required!", Toast.LENGTH_SHORT).show();
            return;
        }
        AndyUtilsRistoratore.showSimpleProgressDialogRistoratore(r_start.this);
        final HashMap<String, String> map = new HashMap<>();
        map.put(AndyConstantsRistoratore.ParamsRistoratore.IDRistoratore, editUserId.getText().toString());
        map.put(AndyConstantsRistoratore.ParamsRistoratore.CELLRistoratore, edtPhone.getText().toString());
        new AsyncTask<Void, Void, String>(){
            protected String doInBackground(Void[] params) {
                String response="";
                try {
                    HttpRequestRistoratore req = new HttpRequestRistoratore(AndyConstantsRistoratore.ServiceTypeRistoratore.LOGINRistoratore);
                    response = req.prepareRistoratore(HttpRequestRistoratore.Method.POST).withDataRistoratore(map).sendAndReadStringRistoratore();
                } catch (Exception e) {
                    response=e.getMessage();
                }
                return response;
            }
            protected void onPostExecute(String result) {
                //do something with response
                Log.d("newwwss", result);
                onTaskCompletedRistoratore(result,LoginTaskRistoratore);
            }
        }.execute();
    }

    private void onTaskCompletedRistoratore(String response,int task) {
        Log.d("responsejson", response.toString());
        AndyUtilsRistoratore.removeSimpleProgressDialogRistoratore();  //will remove progress dialog
        switch (task) {
            case LoginTaskRistoratore:
                if (parseContent.isSuccessRistoratore(response)) {
                    parseContent.saveInfoRistoratore(response);
                    Toast.makeText(r_start.this, "Accesso eseguito", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(r_start.this,RistoratoreHome.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    this.finish();
                }else {
                    Toast.makeText(r_start.this, parseContent.getErrorMessageRistoratore(response), Toast.LENGTH_SHORT).show();
                }
        }
    }

0 Answers