Tuesday, 3 September 2013

How to Update Data using PHP MYSQL in Android

How to Update Data using PHP MYSQL in Android

I am writing an app in which i need to update data using PHP MySQL, and
everytime i am getting message "Update Data Successufully" but whenever i
see into my database not getting any changes in values.
Logcat says:
09-04 11:20:26.732: I/ActivityManager(89): START
{cmp=com.example.appointment/.RescheduleActivity (has extras)} from pid
543
09-04 11:20:26.732: W/WindowManager(89): Failure taking screenshot for
(230x383) to layer 21015
09-04 11:20:26.832: I/WindowManager(89): createSurface Window{41622e28
Toast paused=false}: DRAW NOW PENDING
09-04 11:20:27.131: D/dalvikvm(543): GC_CONCURRENT freed 190K, 3% free
10582K/10887K, paused 5ms+6ms
09-04 11:20:27.992: I/WindowManager(89): createSurface Window{41602718
com.example.appointment/com.example.appointment.RescheduleActivity
paused=false}: DRAW NOW PENDING
09-04 11:20:28.712: D/dalvikvm(142): GC_FOR_ALLOC freed 383K, 7% free
10799K/11527K, paused 86ms
09-04 11:20:29.002: I/ActivityManager(89): Displayed
com.example.appointment/.RescheduleActivity: +2s234ms
09-04 11:20:31.202: I/WindowManager(89): createSurface Window{41603170
AtchDlg:com.example.appointment/com.example.appointment.RescheduleActivity
paused=false}: DRAW NOW PENDING
09-04 11:20:32.833: D/(543): sAppointmentID :: 24
09-04 11:20:32.833: D/(543): sAppointmentID :: 53
09-04 11:20:32.842: I/ActivityManager(89): START
{cmp=com.example.appointment/.UpdateActivity (has extras)} from pid 543
09-04 11:20:32.842: W/WindowManager(89): Failure taking screenshot for
(230x383) to layer 21025
09-04 11:20:33.512: D/(543): resultServer - getByMemberIDR::
{"UserID":"24","AppointmentID":"53","Type":"Cardiology","Date":"12-09-2013","Time":"22:15","Status":"Pending"}
09-04 11:20:33.512: D/(543): strUserID :: 24
09-04 11:20:33.512: D/(543): strAppointmentID :: 53
09-04 11:20:33.523: D/(543): strType :: Cardiology
09-04 11:20:33.523: D/(543): strDate :: 12-09-2013
09-04 11:20:33.523: D/(543): strTime :: 22:15
09-04 11:20:33.612: D/dalvikvm(543): GC_CONCURRENT freed 195K, 4% free
10869K/11207K, paused 9ms+8ms
09-04 11:20:33.693: I/WindowManager(89): createSurface Window{41621998
com.example.appointment/com.example.appointment.UpdateActivity
paused=false}: DRAW NOW PENDING
09-04 11:20:34.442: I/ActivityManager(89): Displayed
com.example.appointment/.UpdateActivity: +1s563ms
09-04 11:20:34.622: D/dalvikvm(142): GC_FOR_ALLOC freed 245K, 7% free
10820K/11527K, paused 52ms
09-04 11:20:38.151: I/WindowManager(89): createSurface Window{4168d938
PopupWindow:41467398 paused=false}: DRAW NOW PENDING
09-04 11:20:41.192: I/WindowManager(89): createSurface Window{4161a468
PopupWindow:413c25a8 paused=false}: DRAW NOW PENDING
09-04 11:20:45.862: D/(543): resultServer - updateData::
{"StatusID":"1","Message":"updated"}
09-04 11:20:45.862: W/System.err(543): org.json.JSONException: No value
for Error
09-04 11:20:45.871: W/System.err(543): at
org.json.JSONObject.get(JSONObject.java:354)
09-04 11:20:45.871: W/System.err(543): at
org.json.JSONObject.getString(JSONObject.java:510)
09-04 11:20:45.871: W/System.err(543): at
com.example.appointment.UpdateActivity.SaveData(UpdateActivity.java:188)
09-04 11:20:45.912: W/System.err(543): at
com.example.appointment.UpdateActivity$1.onClick(UpdateActivity.java:59)
09-04 11:20:45.912: W/System.err(543): at
android.view.View.performClick(View.java:3480)
09-04 11:20:45.912: W/System.err(543): at
android.view.View$PerformClick.run(View.java:13983)
09-04 11:20:45.912: W/System.err(543): at
android.os.Handler.handleCallback(Handler.java:605)
09-04 11:20:45.922: W/System.err(543): at
android.os.Handler.dispatchMessage(Handler.java:92)
09-04 11:20:45.922: W/System.err(543): at
android.os.Looper.loop(Looper.java:137)
09-04 11:20:45.922: W/System.err(543): at
android.app.ActivityThread.main(ActivityThread.java:4340)
09-04 11:20:45.932: W/System.err(543): at
java.lang.reflect.Method.invokeNative(Native Method)
09-04 11:20:45.932: W/System.err(543): at
java.lang.reflect.Method.invoke(Method.java:511)
09-04 11:20:45.941: W/System.err(543): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-04 11:20:45.941: W/System.err(543): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-04 11:20:45.941: W/System.err(543): at
dalvik.system.NativeStart.main(Native Method)
09-04 11:20:45.974: I/ActivityManager(89): START
{cmp=com.example.appointment/.RescheduleActivity} from pid 543
09-04 11:20:45.974: W/WindowManager(89): Failure taking screenshot for
(230x383) to layer 21025
09-04 11:20:46.052: I/WindowManager(89): createSurface Window{415c53f8
Toast paused=false}: DRAW NOW PENDING
09-04 11:20:46.542: I/WindowManager(89): createSurface Window{415d8ff8
com.example.appointment/com.example.appointment.RescheduleActivity
paused=false}: DRAW NOW PENDING
09-04 11:20:47.212: I/ActivityManager(89): Displayed
com.example.appointment/.RescheduleActivity: +1s202ms
09-04 11:22:53.083: D/dalvikvm(175): GC_CONCURRENT freed 458K, 6% free
10347K/10951K, paused 5ms+4ms
: E/(): Device disconnected
PHP Script:
<?php
$objConnect = mysql_connect("localhost","root","");
$objDB = mysql_select_db("android");
$strUserID = $_POST["sUserID"];
$strAppointmentID = $_POST["sAppointmentID"];
$strType = $_POST["sType"];
$strDate = $_POST["sDate"];
$strTime = $_POST["sTime"];
/*** Update ***/
$strSQL = " UPDATE book_appointment SET
Type = '".$strType."',
Date = '".$strDate."',
Time = '".$strTime."'
WHERE AppointmentID = '".strAppointmentID."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot save data!";
}
else
{
$arr['StatusID'] = "1";
$arr['Message'] = "updated";
}
mysql_close($objConnect);
echo json_encode($arr);
?>
UpdateActivity.java:-
public class UpdateActivity extends Activity {
private static final String tag = null;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
showInfo();
// btnSave
Button btnSave = (Button) findViewById(R.id.btnSave);
// Perform action on click
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(SaveData())
{
// When Save Complete
Intent newActivity = new
Intent(UpdateActivity.this,RescheduleActivity.class);
startActivity(newActivity);
}
}
});
// btnCancel
final Button btnCancel = (Button) findViewById(R.id.btnCancel);
// Perform action on click
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new
Intent(UpdateActivity.this,RescheduleActivity.class);
startActivity(newActivity);
}
});
}
public void showInfo()
{
//
txtMemberID,txtUsername,txtPassword,txtConPassword,txtName,txtEmail,txtTel
final TextView tUserID = (TextView)findViewById(R.id.txtUserID);
final TextView tAppointmentID =
(TextView)findViewById(R.id.txtAppointmentID);
final TextView tType = (TextView)findViewById(R.id.txtType);
final TextView tDate = (TextView)findViewById(R.id.txtDate);
final TextView tTime = (TextView)findViewById(R.id.txtTime);
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnCancel = (Button) findViewById(R.id.btnCancel);
String url = "http://10.0.2.2/appointments/getByMemberIDR.php";
Intent intent= getIntent();
final String UserID = intent.getStringExtra("UserID");
final String AppointmentID = intent.getStringExtra("AppointmentID");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sUserID", UserID));
params.add(new BasicNameValuePair("sAppointmentID", AppointmentID));
String resultServer = getHttpPost(url,params);
Log.d(tag, "resultServer - getByMemberIDR:: " + resultServer);
String strUserID = "";
String strAppointmentID = "";
String strType = "";
String strDate = "";
String strTime = "";
JSONObject c;
try {
c = new JSONObject(resultServer);
strUserID = c.getString("UserID");
Log.d(tag, "strUserID :: " + strUserID);
strAppointmentID = c.getString("AppointmentID");
Log.d(tag, "strAppointmentID :: " + strAppointmentID);
strType = c.getString("Type");
Log.d(tag, "strType :: " + strType);
strDate = c.getString("Date");
Log.d(tag, "strDate :: " + strDate);
strTime = c.getString("Time");
Log.d(tag, "strTime :: " + strTime);
if(!strUserID.equals(""))
{
tUserID.setText(strUserID);
tAppointmentID.setText(strAppointmentID);
tType.setText(strType);
tDate.setText(strDate);
tTime.setText(strTime);
}
else
{
tUserID.setText("====");
tAppointmentID.setText("====");
tType.setText("====");
tDate.setText("====");
tTime.setText("====");
btnSave.setEnabled(false);
btnCancel.requestFocus();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean SaveData()
{
// txtMemberID,txtPassword,txtName,txtEmail,txtTel
final EditText txtType = (EditText)findViewById(R.id.txtType);
final EditText txtDate = (EditText)findViewById(R.id.txtDate);
final EditText txtTime = (EditText)findViewById(R.id.txtTime);
//Dialog
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("Error! ");
ad.setIcon(android.R.drawable.btn_star_big_on);
ad.setPositiveButton("Close", null);
String url = "http://10.0.2.2/appointments/updateData.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sType",
txtType.getText().toString()));
params.add(new BasicNameValuePair("sDate",
txtDate.getText().toString()));
params.add(new BasicNameValuePair("sTime",
txtTime.getText().toString()));
String resultServer = getHttpPost(url,params);
Log.d(tag, "resultServer - updateData:: " + resultServer);
/*** Default Value ***/
String strStatusID = "0";
String strMessage = "Unknow Status!";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strMessage = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Prepare Save Data
if(strStatusID.equals("0"))
{
ad.setMessage(strMessage);
ad.show();
return false;
}
else
{
Toast.makeText(UpdateActivity.this, "Update Data Successfully",
Toast.LENGTH_SHORT).show();
}
return true;
}
public String getHttpPost(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new
InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}

No comments:

Post a Comment