字符串在数据库

时间:2016-04-15 01:43:13

标签: java php xml database android-volley

所以我决定为我的应用程序构建登录和注册活动。数据库和服务器已启动并正在运行,应用程序将完美连接到它。我遇到的唯一问题是我发布到数据库的其中一个字段显示为整数而不是预期的字符串。以下是我正在使用的java,xml和php文件:

RegisterActivity.java

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://codeblue.net16.net/Register.php";
    private Map<String, String> params;

    public RegisterRequest(String first_name, String last_name, String email, String password, Response.Listener<String> listener) {
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("first_name", first_name);
        params.put("last_name", last_name);
        params.put("email", email);
        params.put("password", password);
    }

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

}

RegisterRequest.java

import android.app.AlertDialog;
import android.content.Context;
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 android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.example.alex.codeblue.R;

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 editFirstName = (EditText) findViewById(R.id.editFirstName);
        final EditText editLastName = (EditText) findViewById(R.id.editLastName);
        final EditText editEmail = (EditText) findViewById(R.id.editEmail);
        final EditText editPassword = (EditText) findViewById(R.id.editPassword);
        final Button buttonRegister = (Button) findViewById(R.id.buttonRegister);

        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String first_name = editFirstName.getText().toString();
                final String last_name = editLastName.getText().toString();
                final String email = editEmail.getText().toString();
                final String password = editPassword.getText().toString();

                Log.d("first_name", first_name);
                Log.d("last_name", last_name);
                Log.d("email", email);
                Log.d("password", password);

                Response.Listener<String> responseListener = new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        try {
                            //Log.d("JSON Parser", response);
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");


                            if (success) {
                                Context context = getApplicationContext();
                                CharSequence text = "Registration Successful";
                                int duration = Toast.LENGTH_SHORT;

                                Toast toast = Toast.makeText(context, text, duration);
                                toast.show();

                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                RegisterActivity.this.startActivity(intent);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Registration Failed")
                                        .setNegativeButton("Retry", null)
                                        .create()
                                        .show();
                            }

                        } catch (JSONException e) {

                            e.printStackTrace();
                        }


                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(first_name, last_name, email, password, responseListener);
                RequestQueue queue = new Volley().newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });
    }
}

activit_register.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.alex.codeblue.LoginAndRegister.RegisterActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textCapSentences"
        android:id="@+id/editFirstName"
        android:layout_alignParentTop="true"
        android:layout_marginTop="30dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="First Name" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textCapSentences"
        android:id="@+id/editLastName"
        android:layout_below="@+id/editFirstName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="Last Name" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/editEmail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/editLastName"
        android:hint="Email" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/editPassword"
        android:layout_below="@+id/editEmail"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hint="Password" />

    <Button
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="REGISTER"
        android:id="@+id/buttonRegister"
        android:textColor="#ffffff"
        android:background="@color/colorPrimaryDark"
        android:layout_below="@+id/editPassword"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

的login.php

<?php
    $con = mysqli_connect("mysql11.000webhost.com", "(number for 000webhost.com)_user", "(password)", "(number for 000webhost.com)_data");

    $email = $_POST["email"];
    $password = $_POST["password"];

    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE email = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $email, $password);
    mysqli_stmt_execute($statement);

    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $user_id, $first_name, $last_name, $email, $password);

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

    while(mysqli_stmt_fetch($statement)){
        $response["success"] = true;  
        $response["first_name"] = $first_name;
        $response["last_name"] = $last_name;
        $response["email"] = $email;
        $response["password"] = $password;
    }

    echo json_encode($response);
?>

Register.php

<?php
    $con = mysqli_connect("mysql11.000webhost.com", "(number for 000webhost.com)_user", "(password)", "(number for 000webhost.com)_data");

    $first_name = $_POST["first_name"];
    $last_name = $_POST["last_name"];
    $email = $_POST["email"];
    $password = $_POST["password"];

        $statement = mysqli_prepare($con, "INSERT INTO user (first_name, last_name, email, password) VALUES (?, ?, ?, ?)");
        mysqli_stmt_bind_param($statement, "siss", $first_name, $last_name, $email, $password);
        mysqli_stmt_execute($statement);

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

        echo json_encode($response);
?>

现在我遇到的问题是当用户输入&#34; last_name&#34;字段作为字符串,在数据库中它将作为整数输入并显示0.为了测试这个理论,我在&#34; last_name&#34;中输入了一个数字。字段和数字出现在数据库中。其他所有领域似乎都按预期运作。我似乎无法找到错误发生的位置。我一直在寻找解决这个问题的过去一天半,我似乎无法找到错误。有人有什么想法吗?

根据请求,以下是数据库的图片:

Database Tables

Output of Database

1 个答案:

答案 0 :(得分:0)

我认为问题不在代码中,而是在MySQL数据库模式中。请检查您的MySQL数据库列数据类型。他们应该是varchar。

由于您的数据库没问题且需要调试代码。我建议你在插入数据库之前将数据保存在某个文件中来检查你的php代码,如下面的代码所示。

现在我在php代码中发现了错误。在http://webcache.googleusercontent.com/search?q=cache:http://php.net/manual/en/mysqli-stmt.bind-param.php查看mysqli_stmt_bind_param的详细信息以获取详细信息。

你应该包括&#34; ssss&#34;而不是&#34; siss&#34; i - 表示整数,因此姓氏应为整数。你必须把我改为s - 在参数中意味着字符串。

<强> Register.php

<?php
$con = mysqli_connect("mysql11.000webhost.com", "(number for 000webhost.com)_user", "(password)", "(number for 000webhost.com)_data");

$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$email = $_POST["email"];
$password = $_POST["password"];


    $myfile = fopen("checkdata.log", "w") or die("Unable to open file!");
    fwrite($myfile, "\nfirst name= ".$first_name." last name= ".$last_name." email= ".$email." password=".$password." \n");       
    fclose($myfile);

    $statement = mysqli_prepare($con, "INSERT INTO user (first_name, last_name, email, password) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "ssss", $first_name, $last_name, $email, $password);
    mysqli_stmt_execute($statement);

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

    echo json_encode($response);
?>