Android登录注册:无法使用凌空将数据插入数据库

Posted

技术标签:

【中文标题】Android登录注册:无法使用凌空将数据插入数据库【英文标题】:Android Login Register: Cant insert data into a database using volley 【发布时间】:2019-01-22 16:55:53 【问题描述】:

伙计们,我被困在一个坚硬的地方。使用凌空我尝试在 000webhost 的数据库中创建用户。因此我使用这个Register.php

<?php
    $connect = mysqli_connect("localhost", "id6777294_l2w", "123l2w456", "id6777294_l2w");
    
if(isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["password"]) && isset($_POST["grade"]))
    $name = $_POST["name"];
    $email = $_POST["email"];
    $password = $_POST["password"];
    $class = $_POST["grade"];


    $statement = mysqli_prepare($connect, "INSERT INTO user(name, email, password, grade) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "sssi", $name, $email, $password, $grade);
    mysqli_stmt_execute($statement);
    
    $response = array();
    $response["success"] = true;  
    
    echo json_encode($response);
?>

以及以下两个android

public class RegisterActivity extends AppCompatActivity 

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

        final EditText nameID = (EditText) findViewById(R.id.nameID);
        final EditText emailID = (EditText) findViewById(R.id.emailID);
        final EditText passwordID = (EditText) findViewById(R.id.passwordID);
        final EditText gradeID = (EditText) findViewById(R.id.gradeID);
        final Button registerbtn = (Button) findViewById(R.id.registerbtn);

        registerbtn.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 

                //we need to get the data given by the user
                final String name = nameID.getText().toString();
                final String email = emailID.getText().toString();
                final String password = passwordID.getText().toString();
                final int grade = Integer.parseInt(gradeID.getText().toString());

                //now we need a String Listener for the constructor
                Response.Listener<String> responseListener = new Response.Listener<String>() 

                    /**
                     * everything in here happens when the response listener has been executed,
                     * given the response (which comes as a JSON String) from the Register.php.
                     * If the response is not in the form of a JSON format, then JSONException
                     * will catch the error
                     */

                    @Override
                    public void onResponse(String response) 
                        try 
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");

                            //if successful then go back to LoginActivity, if not make an alert
                            if (success)
                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                RegisterActivity.this.startActivity(intent);
                            else 
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Registrierung fehlgeschlagen").setNegativeButton("Wiederhole",null).create().show();

                            


                         catch (JSONException e) 
                            e.printStackTrace();
                        

                    
                ;

                //this actual creates the request we need for registration
                RegisterRequest registerRequest = new RegisterRequest(name, email, password, grade, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            
        );


    

public class RegisterActivity extends AppCompatActivity 

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

        final EditText nameID = (EditText) findViewById(R.id.nameID);
        final EditText emailID = (EditText) findViewById(R.id.emailID);
        final EditText passwordID = (EditText) findViewById(R.id.passwordID);
        final EditText gradeID = (EditText) findViewById(R.id.gradeID);
        final Button registerbtn = (Button) findViewById(R.id.registerbtn);

        registerbtn.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View v) 

                //we need to get the data given by the user
                final String name = nameID.getText().toString();
                final String email = emailID.getText().toString();
                final String password = passwordID.getText().toString();
                final int grade = Integer.parseInt(gradeID.getText().toString());

                //now we need a String Listener for the constructor
                Response.Listener<String> responseListener = new Response.Listener<String>() 

                    /**
                     * everything in here happens when the response listener has been executed,
                     * given the response (which comes as a JSON String) from the Register.php.
                     * If the response is not in the form of a JSON format, then JSONException
                     * will catch the error
                     */

                    @Override
                    public void onResponse(String response) 
                        try 
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");

                            //if successful then go back to LoginActivity, if not make an alert
                            if (success)
                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                RegisterActivity.this.startActivity(intent);
                            else 
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Registrierung fehlgeschlagen").setNegativeButton("Wiederhole",null).create().show();

                            


                         catch (JSONException e) 
                            e.printStackTrace();
                        

                    
                ;

                //this actual creates the request we need for registration
                RegisterRequest registerRequest = new RegisterRequest(name, email, password, grade, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            
        );


    

我没有收到来自 logcat 等的错误或提示。当我点击注册按钮时,我回到了 LoginActivity,它看起来很完美。但是使用 phpmyadmin 检查数据库时,“用户”数据中没有新条目。

请帮忙,我做了所有建议的改进并清除了常见错误,但它不会存储数据...

【问题讨论】:

【参考方案1】:

在您的绑定参数语句中,您传递了参数 $grade,但我没有看到 $grade 变量已初始化,您的意思是要传递 $class 吗?可能是检查的开始。

【讨论】:

Aww man thx这么多,我不小心把旧的php文件上传到000webhost,所以我没有发现错误

以上是关于Android登录注册:无法使用凌空将数据插入数据库的主要内容,如果未能解决你的问题,请参考以下文章

如何用凌空POST jsonobject

Android studio连接MySQL并完成简单的登录注册功能

无法使用 Kotlin Retrofit 将数据从 Android 插入到 Mysql [重复]

我做了一个ASP用户注册页面,本地测试不正常,插入记录后显示错误500,输入的网页无法预览

Android移动应用开发之登录用户密码记住及创建数据库存储查询用户名密码

在注册页面中插入用户名时,它在 phpmyadmin 数据库中变为零 (0)