将Android与PHPMyAdmin连接

时间:2017-06-07 07:28:00

标签: android phpmyadmin

我在将我的android项目连接到phpmyadmin的数据库时遇到了一些麻烦,有人可以帮我解决如何将我的android项目连接到phpmyadmin的问题吗?

这些是我的java代码,只需填写底部的其他代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Activity_SignUp extends AppCompatActivity{

private Button btnSubmit;
private EditText name;
private EditText uname;
private EditText pass;
private EditText jabatan;
private EditText confirm;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);

    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    name = (EditText) findViewById(R.id.Name);
    jabatan = (EditText) findViewById(R.id.Jabatan);
    uname = (EditText) findViewById(R.id.UName);
    pass = (EditText) findViewById(R.id.Pass);
    confirm = (EditText) findViewById(R.id.Confirm);

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onClick_SignUp(v);


        }
    });
}

public void onClick_SignUp(View v){

    if (name.getText().toString().equals("")) {
        Toast.makeText(Activity_SignUp.this, "Name Must be Filled", Toast.LENGTH_SHORT).show();
    } else if (jabatan.getText().toString().equals("")) {
        Toast.makeText(Activity_SignUp.this, "Age Must be Filled", Toast.LENGTH_SHORT).show();
    } else if (uname.getText().toString().equals("")) {
        Toast.makeText(Activity_SignUp.this, "UserName must Be Filled", Toast.LENGTH_SHORT).show();
    } else if (pass.getText().toString().equals("")) {
        Toast.makeText(Activity_SignUp.this, "Password must be Filled", Toast.LENGTH_SHORT).show();
    } else if (confirm.getText().toString().equals("")) {
        Toast.makeText(Activity_SignUp.this, "Confirm Must be Filled", Toast.LENGTH_SHORT).show();
    } else if (!confirm.getText().toString().equals(pass.getText().toString())) {
        Toast.makeText(Activity_SignUp.this, "Confirm Doesn't match", Toast.LENGTH_SHORT).show();
    } else {

    }
}
}`

这是我的php文件:

Service.php

<?php

define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','account');

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

$Nama = $_POST['Nama'];
$Jabatan = $_POST['Jabatan'];
$Username = $_POST['Username'];
$Password = $_POST['Password'];

$sql = "insert into account (Nama,Jabatan,Username,Password) values ('$Nama','$Jabatan','$Username','$Password')";
if(mysqli_query($con,$sql)){
  echo 'success';
}
else{
  echo 'failure';
}
mysqli_close($con);

?>

dbConnect.php

<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','account');

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>

insert.php

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){


$Nama = $_POST['Nama'];
$Jabatan = $_POST['Jabatan'];
$Username = $_POST['Username'];
$Password = $_POST['Password'];

if($Nama == '' || $Jabatan == '' || $Username == '' || $Password == ''){

}else{

require_once('dbConnect.php');

$sql = "SELECT * FROM account WHERE Username='$Username'";

$check = mysqli_fetch_array(mysqli_query($con,$sql));

if(isset($check)){
echo 'Username already exist';
}else{ 
    $sql = "INSERT INTO account (Nama,Jabatan,Username,Password) VALUES('$Nama','$Jabatan','$Username','$Password')";

    if(mysqli_query($con,$sql)){
        echo 'successfully registered';
    }else{
        echo 'oops! Please try again!';
    }
}
    mysqli_close($con);
}
}else{
    echo 'error';
}
?>

2 个答案:

答案 0 :(得分:0)

您不需要通过v

 btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onClick_SignUp();


        }
    });

删除v方法中的onClick_SignUp

答案 1 :(得分:0)

如果您的Apache和MySql正在运行,您可以使用IP地址访问它。 获取运行服务的系统的IP,并将您的Android设备放在同一网络上,而不是http://localhost/ ....使用IP地址 例如,在您的网址配置中http://10.0.2.2/ ....