- Forum posts: 1
Mar 30, 2017, 7:14:25 PM via Website
Mar 30, 2017 7:14:25 PM via Website
I'm a student and I'm trying to insert data into mySql database using android. I created the layout, java class as well as php file for registration. I get no errors when I run the app but once I click Sign up Button, it does not work. No error is shown but it does not continue either.
NOTE: I think it is something wrong with my php file. But I might be wrong too.
Here are my codes.
Register.java
package com.example.mohamedwisam.regtest;
import android.content.Intent; import
android.support.v7.app.AlertDialog; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.widget.Button; import
android.widget.EditText; import android.widget.Toast;import com.android.volley.RequestQueue; import
com.android.volley.Response; import
com.android.volley.toolbox.StringRequest; import
com.android.volley.toolbox.Volley;import org.json.JSONException; import org.json.JSONObject;
public class Register extends AppCompatActivity {
EditText etName, etPwd, etFullname, etDob;
Button btnReg;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); etName = (EditText)findViewById(R.id.edtname); etPwd = (EditText)findViewById(R.id.edtpassword); etFullname = (EditText)findViewById(R.id.edtfullname); etDob = (EditText)findViewById(R.id.edtage); btnReg = (Button)findViewById(R.id.btnreg); btnReg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final String uname = etName.getText().toString(); final String pwd = etPwd.getText().toString(); final String fullname = etFullname.getText().toString(); final String dob = etDob.getText().toString(); Response.Listener<String> responseListener = new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jsonResponse = new JSONObject(response); boolean success = jsonResponse.getBoolean("success"); if(success) { Intent intent = new Intent(Register.this, Login.class); Register.this.startActivity(intent); }else { Toast.makeText(Register.this, "ERROR", Toast.LENGTH_LONG).show(); } } catch (JSONException e) { e.printStackTrace(); } } }; RegisterRequest registerRequest = new RegisterRequest(uname, pwd, fullname, dob, responseListener); RequestQueue queue = Volley.newRequestQueue(Register.this); queue.add(registerRequest); } }); } }
And Here is my RegisterRequest.java
package com.example.mohamedwisam.regtest;
import com.android.volley.Response; import
com.android.volley.toolbox.StringRequest;import java.util.HashMap; import java.util.Map;
/** * Created by mohamedwisam on 3/30/17. */
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "url is here. forum does not let me post it yet";
private Map params;public RegisterRequest(String uname, String password, String name, String dob, Response.Listener<String> listener) { super(Method.POST, REGISTER_REQUEST_URL, listener, null); params = new HashMap<>(); params.put("username", uname); params.put("password", password); params.put("name", name); params.put("dob", dob); } @Override public Map<String, String> getParams() { return params; } }
and my PHP file. Reg.php
$con = mysqli_connect("localhost", "root", "", "medicap");
$name = $_POST["username"]; $age = $_POST["password"]; $username = $_POST["name"]; $password = $_POST["dob"]; $statement = mysqli_prepare($con, "INSERT INTO users (username, password, name, dob) VALUES (?, ?, ?, ?)"); mysqli_stmt_bind_param($statement, 'ssss', $name, $username, $age, $password); mysqli_stmt_execute($statement); $response = array(); $response["success"] = true; echo json_encode($response); ?>
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.