This tutorial describes Update remote data in android system from MySql database using PHP and storing that in layout .
1)First Create Database and table Update in mysql
Update.sql
Create the fields ID ,Email,Mobile No,Hometel,WorkTel
2)Create Update.php file
<?php
mysql_connect("localhost","MYSQL USERNAME","MYSQL PASSWORD");
mysql_select_db("DATABASE NAME");
//$sql=mysql_query("update rcmyinfo set email ='".$_REQUEST['e_mail']."' where medno = '".$_REQUEST['medical_no']."'");
$sql=mysql_query("update rcmyinfo set email ='".$_REQUEST['e_mail']."',worktel = '".$_REQUEST['wk_no']."' ,hometel = '".$_REQUEST['hm_no']."', mobno = '".$_REQUEST[mob_no]."' where medno = '".$_REQUEST['medical_no']."'");
$r=mysql_query($sql);
if(!$r)
echo "Error in query: ".mysql_error();
mysql_close();
?>
3) Create XML File
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#971317" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back"
android:background="#00000000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:textSize="20dp"
android:textColor="#ffffffff" />
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView android:layout_height="25dp"
android:layout_width="wrap_content"
android:text="Medno"
android:textStyle="bold"
android:gravity="center"
android:textSize="15dp"
android:layout_marginLeft="45dp"
/>
<EditText android:layout_height="35dp"
android:layout_width="fill_parent"
android:background="#b22222"
android:id="@+id/editmed"
android:layout_marginLeft="30dp"/>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Email"
android:textStyle="bold"
android:gravity="center"
android:textSize="15dp"
android:layout_marginLeft="45dp"
/>
<EditText android:layout_height="35dp"
android:layout_width="fill_parent"
android:background="#b22222"
android:id="@+id/edit1"
android:layout_marginLeft="40dp"/>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Mobile No"
android:textStyle="bold"
android:gravity="center"
android:textSize="15dp"
android:layout_marginLeft="30dp"
/>
<EditText android:layout_height="35dp"
android:layout_width="fill_parent"
android:background="#b22222"
android:id="@+id/edit2"
android:layout_marginLeft="25dp"/>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Home Tel:"
android:textStyle="bold"
android:gravity="center"
android:textSize="15dp"
android:layout_marginLeft="30dp"
/>
<EditText android:layout_height="35dp"
android:layout_width="fill_parent"
android:id="@+id/edit3"
android:background="#b22222"
android:layout_marginLeft="25dp"/>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Work Tel:"
android:textStyle="bold"
android:gravity="center"
android:textSize="15dp"
android:layout_marginLeft="30dp"
/>
<EditText android:layout_height="35dp"
android:layout_width="fill_parent"
android:id="@+id/edit4"
android:background="#b22222"
android:layout_marginLeft="30dp"/>
</LinearLayout>
<Button
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/save"
android:text="Save"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblResult"
android:textColor="#ff000000"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#ff000000"
android:layout_marginTop="160dp">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#971317"
android:text="Personal"
android:id="@+id/info"
android:drawableTop="@drawable/profile"
android:textColor="#ffffffff"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#971317"
android:text="Medical"
android:id="@+id/medical"
android:textColor="#ffffffff"
android:drawableTop="@drawable/gish"
android:layout_marginLeft="0.1dp"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#971317"
android:text="Update"
android:id="@+id/update"
android:drawableTop="@drawable/info"
android:textColor="#ffffffff"
android:layout_marginLeft="0.1dp"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#971317"
android:textColor="#ffffffff"
android:text="Family"
android:id="@+id/family"
android:drawableTop="@drawable/gish"
android:layout_marginLeft="0.1dp"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#971317"
android:text="Other"
android:textColor="#ffffffff"
android:id="@+id/other"
android:drawableTop="@drawable/update"
android:layout_marginLeft="0.1dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
4)Create Java Class Updateinfonew.java in android project to update data
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
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 android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Updateinfonew extends Activity
{
EditText email,mobile,home,work,medino;
Button save;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
save = (Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new task1().execute();
}
});
}
class task1 extends AsyncTask<String, String, Void>
{
String em,mob,hmtel,wktel,medical;
InputStream is = null ;
protected void onPreExecute()
{
email = (EditText)findViewById(R.id.edit1);
mobile = (EditText)findViewById(R.id.edit2);
home = (EditText)findViewById(R.id.edit3);
work = (EditText)findViewById(R.id.edit4);
medino = (EditText)findViewById(R.id.editmed);
lblResult = (TextView)findViewById(R.id.lblResult);
em = email.getText().toString();
mob = mobile.getText().toString();
hmtel = home.getText().toString();
wktel = work.getText().toString();
medical = medino.getText().toString();
}
@Override
protected Void doInBackground(String... params)
{
try
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("e_mail",em));
nameValuePairs.add(new BasicNameValuePair("mob_no",mob));
nameValuePairs.add(new BasicNameValuePair("hm_no",hmtel));
nameValuePairs.add(new BasicNameValuePair("wk_no",wktel));
nameValuePairs.add(new BasicNameValuePair("medical_no",medical));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.esarmobile.com/newhosp/update.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
return null;
}
protected void postExecute(Void v)
{
lblResult.setText("updation successfull");
Toast.makeText(getBaseContext(), "Updation Successfull", Toast.LENGTH_LONG).show();
//medical = (Button)findViewById(R.id.medical);
//family = (Button)findViewById(R.id.family);
}
}
@Override
public void onBackPressed() {
Intent home = new Intent(getApplicationContext(),Dashboard.class);
startActivity(home);
}
}
I have done application using ASYNCTASK so that it will support all android device if you find problem please comment
No comments:
Post a Comment