Reputation: 4020
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.
Upvotes: 0
Views: 1210
Reputation: 1628
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
Upvotes: 0
Reputation: 152817
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.
Upvotes: 1