Issue in storing data in MySQL database

I am new to android with MySQL and I am facing an issue in storing form data in MySQL database. I am using 000webhost to store my php files. By using volley, I parse the data. There is no error but the data is not getting stored. I have attached the code below:

register.php

<?php      $servername = "localhost";     $username = "id14252713_pranav";     $password = "Pikukumar@9828";     $dbname = "id14252713_rhythmcaster";          $conn = mysqli_connect($servername,$username,$password,$dbname);          $name = $_POST['name'];     $email = $_POST['email'];     $password = $_POST['password'];     $password = password_hash($password, PASSWORD_DEFAULT);     $phonenumber = $_POST['phonenumber'];      $query = "INSERT INTO users(name,email,password,phonenumber) VALUES ('$name','$email','$password','$phonenumber')";       $result = mysqli_query($conn,$query);      if ($result) {         echo "Successfully registered";     }     else{         echo "Failed to register";     }     mysqli_close($conn); ?> 

My java code is

NewAccount.java

    public class NewAccount extends AppCompatActivity {      EditText username, useremail, upass, mobile;     TextInputLayout passwordLayout, nameLayout, emailLayout, phoneLayout;     Button submit;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_new_account);          setSupportActionBar((Toolbar) findViewById(R.id.toolbar));         getSupportActionBar().setDisplayShowHomeEnabled(true);         getSupportActionBar().setDisplayHomeAsUpEnabled(true);          username = (EditText) findViewById(R.id.username);         useremail = (EditText) findViewById(R.id.email);         upass = (EditText) findViewById(R.id.password);         mobile = (EditText) findViewById(R.id.phone);         submit = (Button) findViewById(R.id.signup);          passwordLayout = (TextInputLayout) findViewById(R.id.password_layout);         nameLayout = (TextInputLayout) findViewById(R.id.name_layout);         emailLayout = (TextInputLayout) findViewById(R.id.email_layout);         phoneLayout = (TextInputLayout) findViewById(R.id.phone_layout);          submit.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 registerUser();             }         });      }      private void registerUser() {         final String name = username.getText().toString();         final String email = useremail.getText().toString();         final String password = upass.getText().toString();         final String phonenumber = mobile.getText().toString();          if (name.isEmpty() || email.isEmpty() || password.isEmpty() || phonenumber.isEmpty()){             Toast.makeText(this, "Enter all the details", Toast.LENGTH_SHORT).show();             passwordLayout.setBoxStrokeColor(Color.RED);             phoneLayout.setBoxStrokeColor(Color.RED);             nameLayout.setBoxStrokeColor(Color.RED);             emailLayout.setBoxStrokeColor(Color.RED);         }         else if (password.length() < 8){             upass.setError("Password should be atleast 8 characters");             upass.setFocusable(true);             passwordLayout.setBoxStrokeColor(Color.RED);         }         else if (phonenumber.length() != 10){             phoneLayout.setError("Mobile number should be 10 numbers");             phoneLayout.setFocusable(true);             phoneLayout.setBoxStrokeColor(Color.RED);         }         else if (name.matches("(.*[0-9].*)") || name.matches("(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)")){             username.setError("Name field is invalid.");             username.setFocusable(true);             nameLayout.setBoxStrokeColor(Color.RED);         }         else if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){             useremail.setError("Check Email ID");             useremail.setFocusable(true);             emailLayout.setBoxStrokeColor(Color.RED);         }         else {             String url = "https://rhythmcaster.000webhostapp.com/register.php";             StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {                 @Override                 public void onResponse(String response) {                     if(response.equals("Successfully registered")){                         Toast.makeText(NewAccount.this, "Registered Successfully", Toast.LENGTH_SHORT).show();                     }                     else {                         Toast.makeText(NewAccount.this, response, Toast.LENGTH_SHORT).show();                     }                 }             }, new Response.ErrorListener() {                 @Override                 public void onErrorResponse(VolleyError error) {                     Toast.makeText(NewAccount.this, error.getMessage(), Toast.LENGTH_SHORT).show();                 }             }             ){                 @Override                 protected Map<String, String> getParams() throws AuthFailureError {                     Map<String, String> map = new HashMap<String, String>();                     map.put("name", name);                     map.put("email", email);                     map.put("password", password);                     map.put("phonenumber", phonenumber);                      return map;                 }             };             RequestQueue requestQueue = Volley.newRequestQueue(NewAccount.this);             requestQueue.add(request);         }     } 
Add Comment
1 Answer(s)

First of all, you have to check your API that working or not for check use chrome extension like talent or postmen if data inserted successfully using API then the problem is in your code

I provide some code that would be work for you

 StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {             @Override             public void onResponse(String response) {            Toast.makeText(NewAccount.this, response, Toast.LENGTH_SHORT).show();          }, new Response.ErrorListener() {             @Override             public void onErrorResponse(VolleyError error) {                 Toast.makeText(NewAccount.this, error.getMessage(), Toast.LENGTH_SHORT).show();             }         }) {             @Override             protected Map<String, String> getParams() {                 Map<String, String> parms = new HashMap<String, String>();                  parms.put("name", name);                  parms.put("email", email);                  parms.put("password", password);                  parms.put("phonenumber", phonenumber);                 return parms;             }         };  RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());                     requestQueue.add(stringRequest); 
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.