Building Connection from Android to MySQL

Viewed 106

Hi guys I'm building a Login and Register app from android studio and whenever I run the app I get no errors but the app is not connecting to the PHP itself, I tried to change the address of the URL but everytime I change it, there's an error but whenever I use my IP and the address of the php I got no error but there's no response to the process of php and the process of android, am I using the right address or should I change it?

Here's my code:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

public class Login extends AppCompatActivity {
private EditText etNumber, etPassword;
private String phone, password;
private static final String URL = "http://192.168.1.218/login/login.php";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    phone ="";
    password = "";
    etNumber = findViewById(R.id.username);
    etPassword = findViewById(R.id.password);
}
public void backToMenu (View view){
    phone = etNumber.getText().toString().trim();
    if(!phone.equals("")){
        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.equals("true")) {

                    finish();

                    Intent intent = new Intent(Login.this, MainActivity.class);

                    startActivity(intent);

                } else if (response.equals("false")) {
                    Toast.makeText(Login.this, "Invalid Login ID/Password", Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(Login.this, error.toString().trim(),Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> data = new HashMap<>();
                data.put("phone_number", phone);
                return data;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    }else {
        Toast.makeText(this, "Fields cannot be empty!", Toast.LENGTH_SHORT).show();

    }
}



public void switchRegister (View view){
    Intent intent = new Intent(this, Register.class);
    startActivity(intent);
    finish();
}

And here's my PHP code

  <?php
      if(isset($_POST['phone'])){
      require_once "conn.php";
      require_once "validate.php";
      $phone = $_POST['phone'];
      $sql = "select * from users where phone_number= '$phone'";
      $result = $conn->query($sql);
        if($result->num_rows > 0){
           echo "true";
           session_start();
           $_SESSION['phone_number'] = $row['phone_number'];
            header("location: validatepass.php?phone=$phone");

          }
            else{
    echo "false";
}

 }
 ?>
1 Answers

you should use this code.

 <?php
  
   class Reply{

       function procced(){

           if(isset($_POST['phone']))
           {
           require_once "conn.php";
           require_once "validate.php";
           $phone = $_POST['phone'];
           $sql = "select * from users where phone_number= '$phone'";
           $result = $conn->query($sql);
             if($result->num_rows > 0)
               {
      
                session_start();
                $_SESSION['phone_number'] = $row['phone_number'];
                header("location: validatepass.php?phone=$phone");
                $data=["response"=>"success"];
                return json_encode($data);
               }
           
             else{
               $data=["response"=>"error"];
               return json_encode($data);
             }
         }

         $data=["response"=>"error"];
               return json_encode($data);
     }
Related