1

Data isnt being sent to the php file. Condition if(isset($_POST['f_name]) is not true.

Connection to php file is successful and i tested it, but data isn't being sent.

MainActivity.java

 package com.example.sarah.vent;

 import android.app.ProgressDialog;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Spinner;
 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 org.json.JSONException;
 import org.json.JSONObject;

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

public class MainActivity extends AppCompatActivity {

String arr[]={"public","private"};
String privacy;
EditText fi_name,la_name,password,emai;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button reg=findViewById(R.id.reg);
    fi_name=findViewById(R.id.f_name);
     la_name=findViewById(R.id.l_name);
     password=findViewById(R.id.pass);
     emai=findViewById(R.id.email);
     pd=new ProgressDialog(this);
    reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onclick();
            register();
        }
    });

}


void onclick(){
    final Spinner s=(Spinner) findViewById(R.id.privacy);
    ArrayAdapter<String> m=new ArrayAdapter <String>(this,android.R.layout.simple_spinner_dropdown_item,arr);
    s.setAdapter(m);
    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            i=s.getSelectedItemPosition();
            privacy=arr[i];
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

private void register() {
    final String f_name=fi_name.getText().toString().trim();
    final String l_name=la_name.getText().toString().trim();
    final String email=emai.getText().toString().trim();
    final String pass=password.getText().toString().trim();
    Toast.makeText(this, f_name, Toast.LENGTH_SHORT).show();
    pd.setMessage("Registering user");
    pd.show();

    StringRequest str=new StringRequest(Request.Method.POST, Constants.URL_REGISTER, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            pd.dismiss();
            try {
                JSONObject jsonObject=new JSONObject(response);
                Toast.makeText(MainActivity.this, jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();


            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            pd.dismiss();
            Toast.makeText(MainActivity.this, "not ok", Toast.LENGTH_SHORT).show();

            Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }){
        @Override
        protected Map<String, String> getPostParams() throws AuthFailureError {
          Map <String,String> params=new HashMap<>();
           params.put("f_name",f_name);
            params.put("l_name",l_name);
            params.put("email",email);
            params.put("pass",pass);
            params.put("privacy",privacy);return params;
        }
    };

    RequestQueue rQ= Volley.newRequestQueue(this);
    rQ.add(str);
}}

registerUser.php

         <?php
         require '../includes/DbConnect.php';

         $response= array(); 


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


          if(isset($_POST['f_name']) and isset($_POST['l_name']) && isset($_POST['pass']) && isset($_POST['email'])){

            $f_name=$_POST['f_name'];

            $l_name=$_POST['l_name'];

            $email=$_POST['email'];

            $password=$_POST['pass'];

            if(isset($_POST['privacy_level']))
              $p_l=$_POST['privacy_level']; 
            else 
              $p_l="public";


            function generateRandomString($length = 10) {
              $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
              $charactersLength = strlen($characters);
              $randomString = '';
              for ($i = 0; $i < $length; $i++) {
                $randomString .= $characters[rand(0, $charactersLength - 1)];
              }
              return $randomString;
            }

              $r_s=generateRandomString();
              $sql="INSERT INTO users (email,pass,f_name,l_name,privacy_level,random_username) 
              VALUES ('$email','$password','$f_name','$l_name','$p_l','$r_s')";

              if(mysqli_query($project, $sql)){
               $response['error']= false;
               $response['message']="User registered Successfully";
             } 



           else {

             $response['error']= true ;
             $response['message']="Failed to Register";

} }
      echo json_encode($response);  }
       ?> 

Data is supposed to reach the php file but the result is empty. Although data is successfully being sent through postman.

Umar Abdullah
  • 1,282
  • 1
  • 19
  • 37
  • Please read about [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection). Instead of building queries with string concatenation, use [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) with [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). See [**this page**](https://phptherightway.com/#databases) and [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) for some good examples. – Alex Howansky Jan 15 '19 at 14:52
  • **Never** store plain text passwords. Instead use [`password_hash()`](http://us3.php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://us3.php.net/manual/en/function.password-verify.php). – Alex Howansky Jan 15 '19 at 14:52
  • Well, have you looked into what kind of request is actually made? Isn't postman supposed to help with these exact woes? – mario Jan 15 '19 at 14:53
  • yeah youre right in case for the password and sql injections but im looking forward to insert the data first. – mohammad hisham itani Jan 15 '19 at 14:55
  • yes i tried postman and everything worked just fine – mohammad hisham itani Jan 15 '19 at 14:55
  • I just dont know why data isnt reached in the php file – mohammad hisham itani Jan 15 '19 at 14:56
  • If you don't share your findings (the exact HTTP request), you won't get an answer. Nobody here looks at 100 lines of code to *guess* possible issues. – mario Jan 15 '19 at 19:49

0 Answers0