This is project connection with php using mysql database.
Create database in phpmyadmin(Mysql)
USE db_client;
CREATE TABLE tbl_client2(id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,name VARCHAR(50) NOT NULL,username VARCHAR(50) NOT NULL,password VARCHAR(50) NOT NULL);
CREATE TABLE tbl_client2(id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,name VARCHAR(50) NOT NULL,username VARCHAR(50) NOT NULL,password VARCHAR(50) NOT NULL);
Insert data into tbl_client2
INSERT INTO tbl_client2(name, username, password)VALUES('abc', 'abc', '123');INSERT INTO tbl_client(name, username, password)VALUES('Test', 'test', 'test');
Connection.php
<?php
$servername = "localhost"; //replace it with your database server name
$username = "root"; //replace it with your database username
$password = ""; //replace it with your database password
$dbname = "db_client2";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
login.php
<?PHP
include_once("connection.php");
if( isset($_POST['txtUsername']) && isset($_POST['txtPassword']) ) {
$username = $_POST['txtUsername'];
$password = $_POST['txtPassword'];
$query = "SELECT username, password FROM tbl_client ".
" WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $query);
if($result->num_rows > 0){
//if(isset($_POST['mobile']) && $_POST['mobile'] == "android"){
echo "success";
exit;
// }
//echo "Login successful";
} else{
echo "Login Failed <br/>";
exit;
}
}
?>
<html>
<head>
<title>Login | Vichit </title>
</head>
<body>
<h1>Login Example|<a href="https://www.youtube.com/channel/UCkIW6gubyhq_17tUI6Qgjog">Vichit Developer Android</a></h1>
<form action="<?PHP $_PHP_SELF ?>" method="post">
Username <input type="text" name="txtUsername" value="" /><br/>
Password <input type="password" name="txtPassword" value="" /><br/>
<input type="submit" name="btnSubmit" value="Login"/>
</form>
</body>
</html>
In AndroidManifest you must add:
<uses-permission android:name="android.permission.INTERNET" />
In Android you must add Jar Library:
-Go to Project -> app -> libs -> past library into libs
-Right click on Gson.jar -> add as library
MainActivity
You must write some code:@Override
public void onClick(View v) { HashMap postData = new HashMap(); postData.put("mobile", "android"); postData.put("txtUsername" , edUsername.getText().toString()); postData.put("txtPassword" , edPassword.getText().toString()); PostResponseAsyncTask task = new PostResponseAsyncTask(this,postData); task.execute("http://10.0.3.2/client/login.php"); }
http://localhost/client/login.php : if you work in php bowers, you need to declare this
http://10.0.3.2/client/login.php : if you work in Genymotion, you need to declare this.
http://10.0.3.2/client/login.php : defaultwhen you Make PostResponseAsyncTask, it will give you a Override one method (processFinish).The mehtod, It mean that when the android processing, it call method to use.@Overridepublic void processFinish(String result) { if (result.equals("success")){ Intent intent = new Intent(MainActivity.this,MyAppActivity.class); startActivity(intent); //Toast.makeText(this,"Login Successfully",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"Login Failed",Toast.LENGTH_SHORT).show(); } }Editor by: Vichit Pov

0 Komentar