数据库不存储信息

时间:2017-06-15 11:15:13

标签: java php android

好的,我需要帮助。我是一名新程序员,我正在设计一个登录和注册活动。我的数据库有问题。它不存储输入的信息

注册申请

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 String REGISTER_REQUEST_URL = "http://localhost/Register.php";
private Map<String, String> params;

public RegisterRequest(String name, String password, String email, String gender, Response.Listener<String> listener){
    super(Method.POST, REGISTER_REQUEST_URL,listener,null);
    params = new HashMap<>();
    params.put("name", name);
    params.put("password", password);
    params.put("email", email);
    params.put("gender", gender);
}
@Override
public Map<String,String> getParams(){
    return params;
}

}

注册活动

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.nightwing.naijatycoon.R;
import com.nightwing.naijatycoon.activities.Request.RegisterRequest;
import com.nightwing.naijatycoon.activities.helper.InputValidation;

import org.json.JSONException;
import org.json.JSONObject;


public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
private final AppCompatActivity activity = RegisterActivity.this;

private NestedScrollView nestedScrollView;

private TextInputLayout textInputLayoutEmail;
private TextInputLayout textInputLayoutPassword;
private TextInputLayout textInputLayoutConfirmPassword;
private TextInputLayout textInputLayoutName;

private EditText editTextName;
private EditText editTextEmail;
private EditText editTextPassword;
private EditText editTextConfirmPassword;
EditText date;

DatePickerDialog datePickerDialog;

private Button btn_link_login;
private RadioGroup genderRG;

private TextView signup;

private InputValidation inputValidation;

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

    initViews();
    initListeners();
    initObject();


}
private void initViews() {
    nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);

    textInputLayoutName = (TextInputLayout) findViewById(R.id.LayoutName);
    textInputLayoutEmail= (TextInputLayout) findViewById(R.id.LayoutEmail);
    textInputLayoutPassword= (TextInputLayout) findViewById(R.id.LayoutPassword);
    textInputLayoutConfirmPassword = (TextInputLayout) findViewById(R.id.layout_confirm_password);

    editTextEmail= (EditText) findViewById(R.id.email);
    editTextPassword = (EditText) findViewById(R.id.password);
    editTextConfirmPassword = (EditText) findViewById(R.id.confirm_password);
    editTextName = (EditText) findViewById(R.id.name);


    genderRG = (RadioGroup) findViewById(R.id.gender_radio_group);

    btn_link_login = (Button) findViewById(R.id.btn_link_login);
    signup = (Button) findViewById(R.id.signup);



}
private void initListeners() {
    btn_link_login.setOnClickListener(this);
    signup.setOnClickListener(this);
}

private void initObject() {
    inputValidation= new InputValidation(activity);
}




@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.signup:
            postDataToSQLite();
            break;
        case R.id.btn_link_login:
            finish();
            break;

    }
}

private void postDataToSQLite(){

    if (!inputValidation.isInputEditTextFilled(editTextName, textInputLayoutName, getString(R.string.error_message_name))) {
        return;
    }
    if(!inputValidation.isInputEditTextFilled(editTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))){
        return;
    }
    if(!inputValidation.isEditTextEmail(editTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))){
        return;
    }
    if (!inputValidation.isInputEditTextFilled(editTextPassword, textInputLayoutPassword,getString(R.string.error_message_password))){
        return;
    }
    if(!inputValidation.isInputEditTextMatches(editTextPassword, editTextConfirmPassword, textInputLayoutConfirmPassword, getString(R.string.error_password_match))){
        return;
    }

    final String name= editTextName.getText().toString().trim();
    final String password = editTextPassword.getText().toString().trim();
    final String email = editTextEmail.getText().toString().trim();

    final int selectedId = genderRG.getCheckedRadioButtonId();
    String gender;
    if(selectedId == R.id.female_radio_btn)
        gender = "Female";
    else
        gender = "Male";


    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{
                    Snackbar.make(nestedScrollView, getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG).show();
                    emptyInputEditText();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    RegisterRequest registerRequest = new RegisterRequest(name, password, email, gender, responseListener);
    RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
    queue.add(registerRequest);


}

private void emptyInputEditText() {
    editTextName.setText(null);
    editTextPassword.setText(null);
    editTextConfirmPassword.setText(null);
    editTextEmail.setText(null);
}

}

注册Php以存储数据库

    $con = mysql_connect("localhost", "root", "batman", "database");
    $username=$_POST["username"];
    $password=$_POST["password"];
    $email=$_POST["email"];
    $gender=$_POST["gender"];


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


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



echo json_encode($response);

请帮我查一下。

1 个答案:

答案 0 :(得分:0)

您使用mysql驱动程序连接数据库,但使用mysqli驱动程序插入。你必须做这样的事情。

<?php

$con = mysqli_connect("localhost", "root", "batman", "database");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)"));
mysqli_close($con);