2017-02-03 50 views
-4

美好的一天!在這個程序中,我可以檢索所有,除了它返回的用戶名0變量「Username」在數據庫中返回0(Android&PHP開發)

Image of the Database Result

請參見附件碼

Register.php

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

$name = $_POST["name"]; 
$username = $_POST["username"]; 
$age = $_POST["age"]; 
$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); ?> 

RegisterRequest我所要求的細節。類

public class RegisterRequest extends StringRequest { 

private static final String REGISTER_REQUEST_URL = "https://simonewalter.000webhostapp.com/PharmTechPH/Register.php"; 
private Map<String, String> params; 

//ADD ORDER HERE BITCHEZ 
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("username", username); 
    params.put("age", age + ""); 
    params.put("password", password); 
} 

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

RegisterAct ivty.class

public class RegisterActivity extends AppCompatActivity { 

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


    final EditText etName = (EditText) findViewById(R.id.etName); 
    final EditText etUsername = (EditText) findViewById(R.id.etUsername); 
    final EditText etAge = (EditText) findViewById(R.id.etAge); 
    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); 
     } 
    }); 
}} 
+0

我建議你在文件打印在PHP腳本 – chiappins

+2

用戶名的值**切勿將明文密碼**!請使用PHP的[內置函數](http://jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼安全性。如果您使用的PHP版本低於5.5,則可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。確保你*** [不要越獄密碼](http://stackoverflow.com/q/36628418/1011527)***或在哈希之前使用其他任何清理機制。這樣做*更改密碼並導致不必要的附加編碼。 –

+0

我從來沒有在Android開發過,但這裏看起來不正確'mysqli_stmt_bind_param($ statement,「siss」,$ name,$ username,$ age,$ password);' –

回答

3

您發送的username值是String,並在PHP代碼,你已經宣佈int

更改此:

mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password); 
          ........^//error here 

有了這個:

mysqli_stmt_bind_param($statement, "ssss", $name, $username, $age, $password); 
+0

它發生在我身上的'年齡'可能是一個INT,所以OP可以使用'ssis' –

+0

@JayBlanchard,如圖所示的問題所附的圖像中,年齡完全在int中。問題出在用戶數據類型是'int'而不是'String' – W4R10CK

+0

我完全同意你的觀點,我只是說'ssis'(其中username是一個字符串,而age是一個int)也可以工作。 –