因此,我決定爲我的應用程序構建登錄和註冊活動。數據庫和服務器已啓動並正在運行,應用程序完美地連接到它。我遇到的唯一問題是我發佈到數據庫的其中一個字段顯示爲整數而不是字符串。以下是我與工作的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);
?>
現在我遇到的問題是,當用戶輸入 「姓氏」 字段作爲一個字符串,在數據庫中,它將作爲一個整數輸入,並顯示爲0.爲了測試這個理論,我在「last_name」字段中輸入了一個數字,該數字出現在數據庫中。其他領域似乎按預期工作。我似乎無法找到錯誤發生的地方。我一直在尋找過去的一天半來解決這個問題,我似乎無法找到錯誤。有人有主意嗎?
每請求,數據庫以下是圖片:
是什麼在你的數據庫中的列的數據類型? – Scopey
^如果它是一個整數,那麼你發現你的問題 – bmarkham
對不起,我忘了提到這一點。 last_name的設置與first_name完全相同。他們都是varchars和first_name似乎通過罰款。這就是爲什麼我這麼困惑。 –