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

I am a beginner with Android Studio. I want to build a project that user can register and then login using their own accounts. I follow the Prabeesh tutorial Android - MySQL - 02 - Add data into MySQL Database, https://www.youtube.com/watch?v=cOsZHuu8Qog. Every time click register, which should insert data to the MySQL data, MySQL database doesn't update. Here is the code:

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){

    }
}

Here is the code for register activity:

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();

    }
}

Here is the code for 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();
    }
}

I use wampserver as a localhost, and I can only open my server homepage with localhost:81, instead of localhost. So I am wondering whether I should use this http://10.0.2.2/ as the IP address.

PHP file should work as following: 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');";  
 ?>  

Someone please tell me where I went wrong. Thanks

2个回答

在register.php中,



,你必须添加 mysqli_query </ code> 执行查询。</ p>

 &lt;?php 
require”init。 PHP的“;

$ name = $ _POST [“user”];

$ user_name = $ _POST [“user_name”];

$ user_pass = $ _POST [“user_pass”];

$ sql_query =“插入user_info值('$ name','$ user_name','$ user_pass')”;
mysqli_query($ con,$ sql_query);
?&gt;
</ code> < / pre>
</ div>

展开原文

原文

in register.php, you must add mysqli_query to execute the 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);
 ?>

register.php中



</ p>

$ sql_query =“插入user_info值( '$ name','$ user_name','$ user_pass');“; </ code> </ p>

将列名写入要插入数据的位置。 你错过了这个php脚本中的列名。</ p>
</ div>

展开原文

原文

in your register.php

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

write the column names where you want to insert the data. You're missing the column names in this php script.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐