Android - Can not Send Data to Web Server Database

advertisements

When I directly go to the link of PHP by browser, it creates an empty record in the database. However, when I send data using app, it fails.

Java:

protected String doInBackground(String... params) {
    String reg_url = "http://www.minigameserver.square7.ch/register.php";
    String user_name = params[0];
    String user_ac = params[1];
    String user_pw = params[2];
    try {
        URL url = new URL(reg_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");

        httpURLConnection.setDoOutput(true);
        OutputStream OS = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
        String data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+
                URLEncoder.encode("user_ac","UTF-8")+"="+URLEncoder.encode(user_ac,"UTF-8")+"&"+
                URLEncoder.encode("user_pw","UTF-8")+"="+URLEncoder.encode(user_pw,"UTF-8");
        bufferedWriter.write(data);
        bufferedWriter.flush();
        bufferedWriter.close();
        OS.close();

        httpURLConnection.disconnect();

        return "done!";
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "fail!";
}

When I run the app, the massage "done!" is shown.

So, there are no Exception happening. But, why there are no new record?


I saw someone have tired to register with username of teset. Is he mind to tell who did so that I can trace the post.


When I try the code provided by Arshak. The following error comes out. E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f7d9eaad530

register.php:

<?php
require "db_connect.php";

$user_name = $_POST["user_name"];
$user_ac = $_POST["user_ac"];
$user_pw = $_POST["user_pw"];
$user_pw_enc = md5($user_pw);

$sql = "INSERT INTO user(user_name, user_ac, user_pw) VALUES ('$user_name', '$user_ac', '$user_pw_enc')";

$sql2 = "SELECT * FROM user WHERE user_name = '$user_name'";
$result2 = $conn->query($sql2);

$sql3 = "SELECT * FROM user WHERE user_ac = '$user_ac'";
$result3 = $conn->query($sql3);

if($result2->num_rows == 0 && $result3->num_rows == 0){
if ($conn->query($sql) === TRUE)
        echo "Your account is created successfully!\nYou can login now!";
else
    echo "Error: " . $sql . "<br>" . $conn->error;
}
else {
if($result2->num_rows > 0)
    echo "The user name is used!<br>";
if($result3->num_rows > 0)
    echo "The user account is used!<br>";
}

?>


I have updated your code, see if data gets inserted:

protected String doInBackground(String... params) {
String reg_url = "http://www.minigameserver.square7.ch/register.php";
String user_name = params[0];
String user_ac = params[1];
String user_pw = params[2];
try {
        URL url = new URL(reg_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.connect();
        DataOutputStream OS = new DataOutputStream(httpURLConnection.getOutputStream());
        String data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+
                URLEncoder.encode("user_ac","UTF-8")+"="+URLEncoder.encode(user_ac,"UTF-8")+"&"+
                URLEncoder.encode("user_pw","UTF-8")+"="+ URLEncoder.encode(user_pw,"UTF-8");
        OS .writeBytes(data);
        OS .flush();
        OS.close();

        int responseCode=httpURLConnection.getResponseCode();
        String messageFromServer;
        if (responseCode==200) {
            messageFromServer=fromInputStream(httpURLConnection.getInputStream());
            Log.v("RegistrationMessage ",messageFromServer);
            httpURLConnection.disconnect();
            return messageFromServer;
        }

        httpURLConnection.disconnect();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return "fail!";
}

//Add this method inside your AsyncTask
public String fromInputStream(InputStream stream) throws IOException {
        InputStreamReader streamReader = new
                InputStreamReader(stream);
        BufferedReader bufferedReader = new BufferedReader(streamReader);
        StringBuilder builder = new StringBuilder();
        String tempString = "";
        while ((tempString = bufferedReader.readLine()) != null) {
            builder.append(tempString);
        }
        return builder.toString();
    }