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>";
}
?>