0

I'm trying to upload form data from android java app (Using android studio). I've created php file that handle the server side of the upload, The file using POST method, by receiving from the app the form info, and upload this data to my sql database.

So i'm trying to enter data, the Async process work end connect, there is no error at all but nothing getting in the sql table! i'm not getting any tuple into the mySQL server (to users table).i searched the problem but cant find. Please help me find the bug or how I can detect the problem.

Register.java CODE:

package com.example.user.social;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class Register extends AppCompatActivity {

Button register;
Button back;
EditText username;
EditText password;
EditText email;
EditText name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_register);

    register =      (Button)     findViewById(R.id.register);
    back     =      (Button)     findViewById(R.id.back);
    email    =      (EditText)   findViewById(R.id.editText2);
    name    =       (EditText)   findViewById(R.id.editText);
    username    =   (EditText)   findViewById(R.id.user);
    password    =   (EditText)   findViewById(R.id.pass);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivity(new Intent(getApplicationContext(),Login.class));

        }
    });

    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String user = username.getText().toString().trim();
            String pass = password.getText().toString().trim();
            String emaill = email.getText().toString().trim();
            String namee = name.getText().toString().trim();
            new RegisterInServer().execute(user,pass,emaill,namee);
        }
    });


}

private class RegisterInServer extends AsyncTask<String,Void,Long>
{
    @Override
    protected Long doInBackground(String... params) {

        String username = params[0];
        String email = params[2];
        String name = params[3];
        String password = params[1];

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://idoazulay.byethost16.com/phpcode.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("name", "Name"));
            nameValuePairs.add(new BasicNameValuePair("username", "Username"));
            nameValuePairs.add(new BasicNameValuePair("email", "Idoazzz"));
            nameValuePairs.add(new BasicNameValuePair("password", "87426088"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            String responseBody = EntityUtils.toString(response.getEntity());
            Log.d("Hey",responseBody);

        } catch (Exception e) {
            e.printStackTrace();

            // TODO Auto-generated catch block
        }
        return null;

    }

    @Override
    protected void onPostExecute(Long result) {
    }
}

}

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.user.social.Register"
android:background="#fff">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/user"
    android:weightSum="1">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/user"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:inputType="text"
        android:text="aaa"
        android:textAlignment="center"
        android:textColor="#9e9e9e"
        android:layout_weight="0.15" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pass"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:inputType="text"
        android:text="aaa"
        android:textAlignment="center"
        android:textColor="#9e9e9e"
        android:layout_weight="0.15" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:inputType="text"
        android:text="aaa"
        android:textAlignment="center"
        android:textColor="#9e9e9e"
        android:layout_weight="0.15"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:inputType="text"
        android:text="aaa"
        android:textAlignment="center"
        android:textColor="#9e9e9e"
        android:layout_weight="0.15"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="חזרה"
        android:id="@+id/back"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.05"
        android:layout_marginTop="20dp"
        android:background="#58abff"
        android:textColor="#fff" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="הרשמה"
        android:id="@+id/register"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.05"
        android:layout_marginTop="0dp"
        android:background="#2e8ece"
        android:textColor="#fff" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="8dp"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="0dp"
        android:background="#196ca3"
        android:textColor="#fff" />

</LinearLayout>

PHP:

<?php
    $servername = "*****";
    $username = "*****";
    $password = "*****";
    $dbname = "*****";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }



    $name=$_POST['name'];
    $email = $_POST['username'];
    $username = $_POST['email'];
    $password = $_POST['password'];


    $sql = "INSERT INTO users (id,name,username,email,password)
    VALUES (DEFAULT,'{$name}','{$username}','{$email}','{$password}')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
?> 

SQL users table:

id int(10) No
name varchar(15) No
username varchar(20) No
email varchar(40) No
password varchar(15) No

clarification: if I access the url from my computer chrome browser, this adding a null tupple into the user table, so it worked/ – Ido Azulay 12 mins ago

MORE QUESTION: Is it good way communicate with server with android app? (THE PHP WAY)

Ido Azulay
  • 47
  • 1
  • 7
  • 1
    Your PHP code is wide open to [SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Also, storing plain text passwords is a bad idea. Consider using [password_hash](http://php.net/manual/en/function.password-hash.php) to secure them – Machavity May 09 '16 at 12:17
  • What error are you getting? – Gaurav Dave May 09 '16 at 12:18
  • @Jackhardcastle Might want to make it clear that you're getting this free database from the credentials in the question. You should never post live credentials – Machavity May 09 '16 at 12:18
  • Thx mates, i'm building the simple structure (for communicating a server and learning this subject) without any encryption. thx for the attention. – Ido Azulay May 09 '16 at 12:19
  • Haha I was joking @Machavity, but probably risky on the posters part to post this. – Jack hardcastle May 09 '16 at 12:19
  • this is empty free database, please dont steal the 3 null tuples in users table! :D – Ido Azulay May 09 '16 at 12:20
  • @IdoAzulay assuming this database is on your server there's plenty somebody with lame intentions could do that doesn't include stealing your 3 null tuples. Just a heads up ^^ – Jack hardcastle May 09 '16 at 12:21
  • provide response data. – xAqweRx May 09 '16 at 12:23
  • Looks like your array list size is 2 and you need to send 4 parameters which are Not NULL values on Database. ` List nameValuePairs = new ArrayList(2);` – Gekkula May 09 '16 at 12:25
  • Changed thx but still struggling :( – Ido Azulay May 09 '16 at 12:28
  • You need to check your PHP. It says "New record created successfully" when the url is accessed in browser. (With our any parameters sent) – Gekkula May 09 '16 at 12:41
  • How can i get this message "New record created successfully" from the android and check what happend in the php file? – Ido Azulay May 09 '16 at 12:54
  • Moreover, if I access the url from my computer chrome browser, this adding a null tupple into the user table, so it worked/ – Ido Azulay May 09 '16 at 12:56

0 Answers0