无法使用android studio将数据插入MySQL数据库

时间:2016-04-04 02:14:33

标签: java php android

我是Android Studio的初学者。我想构建一个用户可以注册然后使用自己的帐户登录的项目。我按照Prabeesh教程Android - MySQL - 02 - 将数据添加到MySQL数据库https://www.youtube.com/watch?v=cOsZHuu8Qog。每次点击寄存器,应该将数据插入MySQL数据,MySQL数据库不会更新。 这是代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void userReg(View view){
    startActivity(new Intent(this, Register.class));

}

public void userLogin(View view){

    }
}

以下是注册活动的代码:

public class Register extends Activity {

    EditText etName, etUsername, etPassword;
    String name, user_name, user_pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etName = (EditText)findViewById(R.id.name);
        etUsername = (EditText)findViewById(R.id.new_user_name);
        etPassword = (EditText)findViewById(R.id.new_user_pass);
    }

    public void userReg(View view){

        name = etName.getText().toString();
        user_name = etUsername.getText().toString();
        user_pass = etPassword.getText().toString();

        String method = "register";
        BackgroundTask backgroundTask = new BackgroundTask(this);
        backgroundTask.execute(method,name,user_name,user_pass);
        finish();

    }
}

这是BackgroundTask的代码

public class BackgroundTask extends AsyncTask<String, Void, String> {

    Context ctx;
    BackgroundTask(Context ctx){
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        String reg_url = "http://10.0.2.2/webapp/register.php";
        String login_url = "http://10.0.2.2/webapp/login.php";
        String method = params[0];
        if (method.equals("register")) {
            String name = params[1];
            String user_name = params[2];
            String user_pass = params[3];
            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                //httpURLConnection.setDoInput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                        URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
                        URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                //httpURLConnection.connect();
                httpURLConnection.disconnect();
                return "Registration Success...";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }
}

我使用wampserver作为localhost,我只能使用localhost:81而不是localhost打开我的服务器主页。所以我想知道是否应该使用此http://10.0.2.2/作为IP地址。

PHP文件应该如下工作: 的init.php:

<?php  
 $db_name = "webappdb";  
 $mysql_user = "root";  
 $mysql_pass = "root";  
 $server_name = "localhost";  
 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);  
 ?>  

register.php:

<?php  
 require "init.php";  
 $name = $_POST["user"];  
 $user_name = $_POST["user_name"];  
 $user_pass = $_POST["user_pass"];  
 $sql_query = "insert into user_info values('$name','$user_name','$user_pass');";  
 ?>  

有人请告诉我哪里出错了。感谢

2 个答案:

答案 0 :(得分:0)

在register.php中,您必须添加mysqli_query才能执行查询。

<?php  
 require "init.php";  
 $name = $_POST["user"];  
 $user_name = $_POST["user_name"];  
 $user_pass = $_POST["user_pass"];  
 $sql_query = "insert into user_info values('$name','$user_name','$user_pass')";
mysqli_query($con,$sql_query);
 ?>

答案 1 :(得分:0)

在你的register.php

$sql_query = "insert into user_info values('$name','$user_name','$user_pass');";

写下要插入数据的列名。你错过了这个php脚本中的列名。