So I decided to build login and register activities for my application. The database and server are up and running and the application connects to it perfectly. The only issue that I am running into is that one of the fields that I am posting to the database is showing as an integer instead of a string as intended. The following are the java, xml, and php files that I am working with:
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);
?>
Now the issue that I am running into is that when the user inputs the "last_name" field as a string, in the database it will be entered as an integer and will show a 0. To test this theory I entered a number in the "last_name" field and that number showed up in the database. Every other field seems to be working as intended. I can't seem to find where the error is occuring. I have been searching for the past day and a half for the solution to this problem and I can't seem to find the error. Anyone have any ideas?
Per request, the following are pictures of the database:
I think the problem is not in the code but in your MySQL database schema. Please check your MySQL database columns data type. They should be varchar.
Since your database is OK and need to debug your code. I'd suggested you to check your php code by saving data in some file before inserting into database as following code.
Now I found the error in the php code. look detail of mysqli_stmt_bind_param at http://webcache.googleusercontent.com/search?q=cache:http://php.net/manual/en/mysqli-stmt.bind-param.php for detail.
you should include "ssss" instead of "siss" i - means integer so the last name is expected as integer. you must change i to s - means string in arguments.
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, "
first name= ".$first_name." last name= ".$last_name." email= ".$email." password=".$password."
");
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);
?>