I want to build app that users can regist a username and password and the data send to MySQL database using PHP this is my PHP code
$connect = mysqli_connect("mysql1.000webhost.com","a5900443_jekn","123456","a5900443_jek");
if(mysqli_connect_errno($connect))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "success";
}
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
$query = mysqli_query($connect, "insert into users (username, password) values ('$username' ,'$password') ");
mysqli_close($connect);
?>
I try different Android project but the data doesn't save in database can you give me how to send the username and password from the Android app to this PHP code?
This is my Android Java:
public class MainActivity extends Activity{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
private EditText username;
private EditText password;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username = (EditText) findViewById(R.id.e1);
password = (EditText) findViewById(R.id.e2);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
String result = null;
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username",username.getText().toString
()));
nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString
()));
StrictMode.setThreadPolicy(policy);
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://fkrucusowtcl.web44.net/register.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONObject json_data = new JSONObject(result);
CharSequence w= (CharSequence) json_data.get("re");
Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
});
}
Where is the wrong it doesn't send data to PHP