0

I am new to android development. I am making an app where the user needs to register themselves from the app itself. The problem that I get is I can't insert the record once again after it is inserted once. In other words, I am failing to insert the records if i insert one time from my device. I need to wait for atleast 15 min to insert another record. I am using PHP PDO to insert the records in the mysql database.

Here's the php code:

<?php
$nm = $_GET['nm'];
$email = $_GET['email'];
$password = $_GET['pass'];

$con = new PDO("mysql:host=hostname; dbname=database", "username", "passwoprd");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    try {
        $query = $con->prepare("INSERT INTO Entrepreneurs(entFullName, entEmailId, entPassword)
                VALUES(:name, :email, :passWord)");
        $query->bindParam(':name', $nm);
        $query->bindParam(':email', $email);
        $query->bindParam(':passWord', $password);
        $query->execute();
    }catch(PDOException $ex) {
        echo "Some Exception Occured: <br />" . $ex->getMessage();
    }
?>

Here's the java code:

package com.example.entrepreneurexpress.entrepreneurs;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.entrepreneurexpress.R;

public class EntrepreneurRegister extends Activity {

    String extracted_email, extracted_password, extracted_fullName;
    EditText YourName, email, password;
    ProgressDialog pDialog;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entrepreneur_register);

        ActionBar aBar = getActionBar();
        aBar.setDisplayHomeAsUpEnabled(true);

        Button btnEntClear = (Button) findViewById(R.id.btnEntRegClear);
        Button btnEntRegister = (Button) findViewById(R.id.btnEntRegRegister);

        YourName = (EditText) findViewById(R.id.txtEntRegFullName);
        email = (EditText) findViewById(R.id.txtEntRegEmailId);
        password = (EditText) findViewById(R.id.txtEntRegPassword);

        btnEntClear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (YourName.length() >= 1) {
                    YourName.setText("");
                }
                if (email.length() >= 1) {
                    email.setText("");
                }
                if (password.length() >= 1) {
                    password.setText("");
                }
            }           
        });

        btnEntRegister.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(YourName.length() < 1 || email.length() < 1 || password.length() < 1) {
                    Toast.makeText(getApplicationContext(), "Please Fill In All The Details", Toast.LENGTH_LONG).show();
                } else {
                    new RegisterTask().execute("register");
                }
            }
        });
    }

    class RegisterTask extends AsyncTask<String,Void,String>{

        protected void onPreExecute(){
            pDialog = new ProgressDialog(EntrepreneurRegister.this);
            pDialog.setTitle("Processing..");
            pDialog.setMessage("Registering Yourself With Us.......");
            pDialog.setCancelable(true);
            pDialog.setIndeterminate(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params){
            // TODO Auto-generated method stub
            extracted_email = email.getText().toString();
            extracted_fullName = YourName.getText().toString();
            extracted_password = password.getText().toString();

            String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + extracted_fullName + 
                    "&email=" + extracted_email + "&pass=" + extracted_password;

            try {
                HttpClient client = new DefaultHttpClient();
                URI website;
                website = new URI(requesturl);
                HttpGet request = new HttpGet();
                request.setURI(website);
                client.execute(request);
            } catch (IOException e) {
                e.printStackTrace();
            } catch(URISyntaxException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            if(pDialog != null) {
                pDialog.dismiss();
            }
            Toast.makeText(getApplicationContext(), "Sucessfully Inserted !", Toast.LENGTH_LONG).show();
            Intent i = new Intent(getApplicationContext(), WelcomeEntrepreneur.class);
            startActivity(i);
        }
    }
}

UPDATE

I came to know what the error is:

When I type the name in android application with spaces like FirstName LastName, it does not get inserted but when I don't insert any space while typing name like FirstNameLastName, it gets inserted.

Now the question is: How do I solve above error ?

Kindly help me with this query. Thanks.

2 Answers 2

1

You need to URL encode the inputs you pass in your URL strings. Change this

String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + extracted_fullName + 
                "&email=" + extracted_email + "&pass=" + extracted_password;

to something like

String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=" + URLEncoder.encode(extracted_fullName= + 
                "&email=" + URLEncoder.encode(extracted_email) + "&pass=" + URLEncoder.encode(extracted_password);

Also consider changing the HTTP verb from GET to e.g. POST. GET is semantically wrong for a request that modifies something.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude.. you saved my day. Can you please post the tutorial link for inserting data from android using PHP PDO having $_POST method and not $_GET, if you have any.
0

check this PHP-PDO file output in localhost using POSTMan tool. Try this link.PHP - PDO

change php file code

 <?php
 $nm = $_POST['nm'];
  $email = $_POST['email'];
  $password = $_POST['pass'];

  $con = new PDO("mysql:host=hostname; dbname=database", "username", "passwoprd");
  $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {
    $query = $con->prepare("INSERT INTO Entrepreneurs(entFullName, entEmailId, entPassword)
            VALUES(:name, :email, :passWord)");
    $query->bindParam(':name', $nm);
    $query->bindParam(':email', $email);
    $query->bindParam(':passWord', $password);
    $query->execute();
}catch(PDOException $ex) {
    echo "Some Exception Occured: <br />" . $ex->getMessage();
}
?>

and in java code, where you used requesturl...change it..

     String requesturl = "http://mehul.wink.ws/insertEntrepreneur.php?nm=extracted_fullName&email=extracted_email&pass=extracted_password"

Try this

4 Comments

If I insert manually by typing in the URL, it gets inserted. But if I insert from the app. it's not getting inserted.
try this...."mehul.wink.ws/insertEntrepreneur.php?" +"&nm=" + extracted_fullName + "&email=" + extracted_email + "&pass=" + extracted_password;
the output is as follows: extracted_fullName extracted_email extracted_password in the database
Integrity constraint violation: 1048 Column 'entFullName' cannot be null. i have found...You are passing null from Android. $nm is empty,null or 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.