I'm making a little MySql Crud using Android-Studio and the data fetching is working just well but I cannot insert into db and I think the problem may be because of lack of permission to write (maybe?).
Anyway, in a given point, the user is at Register.java Activity
and presses button to register. This calls the method onClick()
on switch case R.id.BtRegs
. This calls a registerUser
method which calls the ServerRequests class
. In the doInBackground
I try
to connect to my localhost using the IP of my network lan calling the Register.php
with POST
method.
It does not return anything from Register.php.
How do I check if it is indeed a lack of permission of some other thing? I think it may be no permission because the same methods used to fetch data worked well.
Here is the part of the code on ServerRequests class
which does the connection:
@Override
protected Void doInBackground(Void... params) {
Map<String,String> dataToSend = new HashMap<>();
dataToSend.put("name", user.name);
dataToSend.put("age", String.valueOf(user.age));
dataToSend.put("username", user.username);
dataToSend.put("password", user.password);
String encodedStr = getEncodedData(dataToSend);
BufferedReader reader = null;
try{
URL url = new URL(SERVER_ADDRESS + "/Register.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(CONNECTION_TIMEOUT);
con.setReadTimeout(CONNECTION_TIMEOUT);
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(encodedStr);
writer.flush();
And this is the Register.php
which does the MySQL insert:
<?php
$con = mysqli_connect("host", "user", "pass", "db");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO tabuser (name, age, username, password) VALUES (?,?,?,?) ");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
note: Considering SERVER_ADDRESS = "http://192.168.x.x"
and $con = mysqli_connect("host", "user", "pass", "db");
is normally set on main proj.