如何在mysql上修改零(0)用户名

时间:2016-06-19 16:33:23

标签: java android

这是我的问题,每次我尝试注册时,MySQL都会输入" 0"的用户名字段。这是我的表:

MySQL Table

RegisterActivity.class

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.app.laundry.crudtest1.Help.RegisterRequest;
import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

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

        final EditText etAge = (EditText) findViewById(R.id.etAge);
        final EditText etName = (EditText) findViewById(R.id.etName);
        final EditText etUsername = (EditText) findViewById(R.id.etUsername);
        final EditText etPassword = (EditText) findViewById(R.id.etPassword);
        final Button bRegister = (Button) findViewById(R.id.bRegister);

        bRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String name = etName.getText().toString();
                final String username = etUsername.getText().toString();
                final int age = Integer.parseInt(etAge.getText().toString());
                final String password = etPassword.getText().toString();

                Response.Listener<String responseListener = new Response.Listener<String() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            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("Register Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(name, username, age, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });
    }
}

RegisterRequest.class

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;    
import java.util.HashMap;
import java.util.Map;

public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://10.0.2.2/ai/Register.php";
    private Map<String, String> params;

    public RegisterRequest(String name, String username, int age, String password, Response.Listener<String> listener) {
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("age", age + "");
        params.put("username", username);
        params.put("password", password);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

Register.php

<?php
     $con = mysqli_connect("localhost", "root", "", "ae");

     $name = $_POST["name"];
     $age = $_POST["age"];
     $username = $_POST["username"];
     $password = $_POST["password"];

     $statement = mysqli_prepare($con, "INSERT INTO user (name, username, age, password) VALUES (?, ?, ?, ?)");
     mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);
     mysqli_stmt_execute($statement);

     $response = array();
     $response["success"] = true;  

     echo json_encode($response);
?>

Rester2.php

<?php
   require("password.php");

   $connect = mysqli_connect("my_host", "my_user", "my_password", "my_database");

   $name = $_POST["name"];
   $age = $_POST["age"];
   $username = $_POST["username"];
   $password = $_POST["password"];

    function registerUser() {
       global $connect, $name, $age, $username, $password;
       $passwordHash = password_hash($password, PASSWORD_DEFAULT);
       $statement = mysqli_prepare($connect, "INSERT INTO user (name, age, username, password) VALUES (?, ?, ?, ?)");
       mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $passwordHash);
       mysqli_stmt_execute($statement);
       mysqli_stmt_close($statement);     
   }

   function usernameAvailable() {
       global $connect, $username;
       $statement = mysqli_prepare($connect, "SELECT * FROM user WHERE username = ?"); 
       mysqli_stmt_bind_param($statement, "s", $username);
       mysqli_stmt_execute($statement);
       mysqli_stmt_store_result($statement);
       $count = mysqli_stmt_num_rows($statement);
       mysqli_stmt_close($statement);
   }

   $response = array();
   $response["success"] = false;  

   if (usernameAvailable()){
       registerUser();
       $response["success"] = true;  
   }

   echo json_encode($response);
?>

1 个答案:

答案 0 :(得分:2)

在Register.php中,它应该是ssis

 $statement = mysqli_prepare($con, "INSERT INTO user (name, username, age, password) VALUES (?, ?, ?, ?)");
 mysqli_stmt_bind_param($statement, "ssis", $name, $username, $age, $password);

只是一个拼写错误,在RegisterActivity类中,它应该是

Response.Listener<String> responseListener 

编辑:字符串siss表示string int string string。因此,它为整数提供了值0。更改为ssss,即string string string string,会修复此问题。