This question already has an answer here:
I'm using android studio and connecting to a database. I've managed to retrieve information from the database and created a log in feature which is working fine. I'm now trying to work on creating a register feature. To register the user only needs to enter their forename, surname, username and password. I've been trying to follow a tutorial from simplified coding but for some reason I can't get it to work. In the code a toast is being used to display an error message if one is returned from the php script but with my code the toast message just displays
This is the php script I'm using for this feature
<?php
$forename = $_GET['forename'];
$surname = $_GET['surname'];
$username = $_GET['username'];
$password = $_GET['password'];
$dob = $_GET['dob'];
if($forename == '' || $surname == '' || $username == '' || $password == '' || $dob == ''){
echo 'please fill all values';
}else{
require_once('dbConnect.php');
$sql = "SELECT * FROM users WHERE username='$username'";
$check = mysqli_fetch_array(mysqli_query($con,$sql));
if(isset($check)){
echo 'username already exist';
}else{
$sql = "INSERT INTO users (forename,surname,username,password,dob) VALUES('$forename','$surname','$username','$password','$dob')";
if(mysqli_query($con,$sql)){
echo 'successfully registered';
}else{
echo 'oops! Please try again!';
}
}
mysqli_close($con);
}
?>
And this is the relevant parts of my java code within the application
@Override
public void onClick(View v){
if(v == buttonReg){
registerUser();
}
}
public void registerUser(){
String forename = eTxtForename.getText().toString().trim().toLowerCase();
String surname = eTxtSurname.getText().toString().trim().toLowerCase();
String username = eTxtUserName.getText().toString().trim().toLowerCase();
String password = eTxtPass.getText().toString().trim().toLowerCase();
register(forename, surname, username, password);
}
private void register(String forename, String surname, String username, String password){
String urlSuffix = "?forename="+forename+"&surname="+surname+"&username="+username+"&password="+password;
class RegisterUser extends AsyncTask<String, Void, String>{
ProgressDialog loading;
@Override
protected void onPreExecute(){
super.onPreExecute();
loading = ProgressDialog.show(RegisterActivity.this, "Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String s){
super.onPostExecute(s);
loading.dismiss();
Log.d("From s", s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params){
String s = params[0];
BufferedReader bufferedReader = null;
try{
URL url = new URL(Config.URL_REGISTER+s);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result = bufferedReader.readLine();
return result;
}catch (Exception e){
return null;
}
}
}
RegisterUser ru = new RegisterUser();
ru.execute(urlSuffix);
}
I think that's everything that you will need to see, but let me know if I need to include anything else
EDIT: As suggested I've used HttpRequester to send a get request to see what the error I'm getting is. The error says:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/u613295501/public_html/register.php on line 14
so line 14 is this line
$check = mysqli_fetch_array(mysqli_query($con,$sql));
</div>