I am developing a app which insert data to MySQL data through PHP. Everything seems Ok. But main problem is The data which i insert goes blank to MySQL database.
Here is my code ......
1) This is my PHP file named insert.php
<?php
$host='my ip address';
$uname='user';
$pwd='password';
$db="user";
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
//$party=$_POST['party'];
//$code=$_POST['code'];
//$emply=$_POST['emply'];
//$date=$_POST['date'];
//echo $code+' '+'aaa';
mysql_query("INSERT INTO myapp(party,code,emply,date) VALUES('$_REQUEST[party]','$_REQUEST[code]','$_REQUEST[emply]','$_REQUEST[date]') ");
//mysql_query("insert into myapp(code) values($code)");
mysql_close($con);
?>
This in "//" blocked means i tried to that 2) This is my java code using (Android Studio)
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MainActivity extends Activity {
protected static final String PrgUpdate = null;
Button mysql;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Button mysql = (Button)findViewById(R.id.mysql);
InputStream is = null;
mysql.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String party="Abhisheck";
String code="All update";
String emply="Bhavasar";
String date="01/04/2014";
List<NameValuePair> nameValuePair= new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("party",party));
nameValuePair.add(new BasicNameValuePair("code",code));
nameValuePair.add(new BasicNameValuePair("emply",emply));
nameValuePair.add(new BasicNameValuePair("date",date));
System.out.println(nameValuePair.toString());
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://youripaddress.com/insert.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Toast.makeText(getApplicationContext(),"Data enter succesful",Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException","Client");
e.printStackTrace();
} catch (IOException e) {
Log.e("Ioexception","Ioexption");
e.printStackTrace();
}
}
});
}
Don't Worry About """"Date (Field)""""" it is character field. My database mysql is here
1 id int(6) UNSIGNED No None AUTO_INCREMENT
2 party char(50) latin1_swedish_ci Yes NULL
3 code varchar(50) latin1_swedish_ci Yes NULL
4 emply varchar(50) latin1_swedish_ci Yes NULL
5 date char(50) latin1_swedish_ci Yes NULL
Now What the actual problem. Why records not insert in MySQL.
Insert
is the class which inserts the data in the database asynchronously. You can call the class by calling the execute()
method. You need to override the doInBackground()
method of AsyncTask
. You can find more details in the documentation over here: AsyncTask
class Insert extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String party="Abhisheck";
String code="All update";
String emply="Bhavasar";
String date="01/04/2014";
List<NameValuePair> nameValuePair= new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("party",party));
nameValuePair.add(new BasicNameValuePair("code",code));
nameValuePair.add(new BasicNameValuePair("emply",emply));
nameValuePair.add(new BasicNameValuePair("date",date));
System.out.println(nameValuePair.toString());
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/insert.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//Toast.makeText(getApplicationContext(),"Data enter succesful",Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException","Client");
e.printStackTrace();
} catch (IOException e) {
Log.e("Ioexception","Ioexption");
e.printStackTrace();
}
return null;
}
}
public class MainActivity extends Activity {
protected static final String PrgUpdate = null;
Button mysql;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Button mysql = (Button)findViewById(R.id.mysql);
ProgressDialog dialog;
InputStream is = null;
mysql.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Insert().execute();
}
});
}
}
Use this inside doInbackground()
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.