duangangpin078794
2019-03-16 23:47
浏览 69

我在将android项目中的字符串值插入在线数据库时遇到问题

Basically I have 4 classes: 2 of them are irrelevant, a php file and an online database on 000webhost that use myphpadmin to manage mysql database.
Everything is working perfectly, except the second string value $username is not inserted and every single variable is similarly processed.

This is my first class Register

package com.example.allrateform;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 org.json.JSONException;
import org.json.JSONObject;

public class Register extends AppCompatActivity {

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

        //CREATING ALL INSTANCES
        final EditText FullnameEditText = (EditText) findViewById(R.id.FullnameEditText);
        final EditText UsernameEditText = (EditText) findViewById(R.id.UsernameEditText);
        final EditText EmailEditText = (EditText) findViewById(R.id.EmailEditText);
        final EditText PasswordEditText = (EditText) findViewById(R.id.PasswordEditText);
        final EditText RetypePasswordEditText = (EditText) findViewById(R.id.RetypePasswordEditText);

        final Button SignupButton = (Button) findViewById(R.id.SignupButton);

        //STORE VARIABLES FROM EDIT TEXTS
        SignupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String fullname = FullnameEditText.getText().toString();
                final String username = UsernameEditText.getText().toString();
                final String email = EmailEditText.getText().toString();
                final String password = PasswordEditText.getText().toString();


                //GET THE RESPONSE AND START CATEGORIES
                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(Register.this, Categories.class);
                                startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
                                builder.setMessage("Register failed")
                                        .create()
                                        .show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(fullname, username, email, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(Register.this);
                queue.add(registerRequest);
            }
        });

    }
}

And this is my second class RegisterRequest

package com.example.allrateform;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 org.json.JSONException;
import org.json.JSONObject;

public class Register extends AppCompatActivity {

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

        //CREATING ALL INSTANCES
        final EditText FullnameEditText = (EditText) findViewById(R.id.FullnameEditText);
        final EditText UsernameEditText = (EditText) findViewById(R.id.UsernameEditText);
        final EditText EmailEditText = (EditText) findViewById(R.id.EmailEditText);
        final EditText PasswordEditText = (EditText) findViewById(R.id.PasswordEditText);
        final EditText RetypePasswordEditText = (EditText) findViewById(R.id.RetypePasswordEditText);

        final Button SignupButton = (Button) findViewById(R.id.SignupButton);

        //STORE VARIABLES FROM EDIT TEXTS
        SignupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String fullname = FullnameEditText.getText().toString();
                final String username = UsernameEditText.getText().toString();
                final String email = EmailEditText.getText().toString();
                final String password = PasswordEditText.getText().toString();


                //GET THE RESPONSE AND START CATEGORIES
                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(Register.this, Categories.class);
                                startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
                                builder.setMessage("Register failed")
                                        .create()
                                        .show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(fullname, username, email, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(Register.this);
                queue.add(registerRequest);
            }
        });

    }
}

And this is my php file

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

$fullname = @($_POST['fullname']);
$username = @($_POST['username']);
$email = @($_POST['email']);
$password = @($_POST['password']);

$statement = mysqli_prepare($con, "INSERT INTO id8963226_user(fullname,username,email,password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $fullname, $username, $email, $password);
mysqli_stmt_execute($statement);

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

echo json_encode($response);?>
}

I tried to think outside the box like considering missing packages, database issues, anything; I already checked everything I could about the syntax. However if you do find any kind of error please leave a comment.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzong2017 2019-03-17 00:01
    已采纳

    Isn't username a string, I think you're biding it as an int

    mysqli_stmt_bind_param($statement, "siss", $fullname, $username, $email, $password);
    mysqli_stmt_execute($statement);
    
    
    点赞 打赏 评论

相关推荐 更多相似问题