2016-04-15 67 views
1

因此,我決定爲我的應用程序構建登錄和註冊活動。數據庫和服務器已啓動並正在運行,應用程序完美地連接到它。我遇到的唯一問題是我發佈到數據庫的其中一個字段顯示爲整數而不是字符串。以下是我與工作的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」字段中輸入了一個數字,該數字出現在數據庫中。其他領域似乎按預期工作。我似乎無法找到錯誤發生的地方。我一直在尋找過去的一天半來解決這個問題,我似乎無法找到錯誤。有人有主意嗎?

每請求,數據庫以下是圖片:

Database Tables

Output of Database

+1

是什麼在你的數據庫中的列的數據類型? – Scopey

+0

^如果它是一個整數,那麼你發現你的問題 – bmarkham

+0

對不起,我忘了提到這一點。 last_name的設置與first_name完全相同。他們都是varchars和first_name似乎通過罰款。這就是爲什麼我這麼困惑。 –

回答

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的詳細信息。

你應該包含「ssss」而不是「siss」i - 意味着整數,所以最後的名字應該是整數。你必須將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); 
?> 
+0

對不起,我忘了提到這一點。 last_name的設置與first_name完全相同。他們都是varchars和first_name似乎通過罰款。這就是爲什麼我這麼困惑。 –

+0

哦,那很奇怪。如果數據類型也是varchar,那應該沒問題。 –

+0

如何創建新的數據庫並再次測試? –

相關問題