php - switch button with mysql update function -
i programming app user can switch toggle "ja" (engl. yes) , "nein" (engl. no) start activity (training/spiel). layout works fine, switch position gets saved when leaving activity or app.
but how can implement mysql
database update if toggle set "ja" or "nein"? if switchspiel
positioned @ "ja", want execute
update users set spiel_anmel = '$training' firstname ='$name'
any ideas? many in advance.
public class abmeldung extends activity { button zurueck; private textview textviewspiel; private textview textviewtraining; private switch switchspiel; private switch switchtraining; string firstname; string spiel_anmel; string spiel_anmel1; inputstream = null; string result = null; string line = null; int code; private string urlt = "http://hots/t.php"; private string urls = "http://host/s.php"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.abmeldung); textviewspiel = (textview) findviewbyid(r.id.textviewspiel); switchspiel = (switch) findviewbyid(r.id.switchspiel); textviewtraining = (textview) findviewbyid(r.id.textviewtraining); switchtraining = (switch) findviewbyid(r.id.switchtraining); sharedpreferences sharedprefs1 = getsharedpreferences("switchspiel", mode_private); switchspiel.setchecked(sharedprefs1.getboolean("valueswitchspiel", true)); sharedpreferences sharedprefs2 = getsharedpreferences("switchtraining", mode_private); switchtraining.setchecked(sharedprefs2.getboolean("valueswitchtraining", true)); zurueck = (button) findviewbyid(r.id.zurueck); zurueck.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { startactivity(new intent(abmeldung.this, main.class)); } }); /** * import varbiable firstn aus sqlite für mysql update **/ databasehandler db = new databasehandler(getapplicationcontext()); /** * hashmap load data sqlite database **/ hashmap<string, string> user = new hashmap<string, string>(); user = db.getuserdetails(); final string firstn = user.get("fname"); // spiel //set switch on //switchspiel.setchecked(true); //attach listener check changes in state switchspiel.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { sharedpreferences.editor editor = getsharedpreferences("switchspiel", mode_private).edit(); editor.putboolean("valueswitchspiel", true); editor.commit(); textviewspiel.settext("dabei!"); spiel_anmel1 = "ja"; } else { sharedpreferences.editor editor = getsharedpreferences("switchspiel", mode_private).edit(); editor.putboolean("valueswitchspiel", false); editor.commit(); textviewspiel.settext("nicht dabei :-(."); spiel_anmel1 = "nein"; } } }); //check current state before display screen if (switchspiel.ischecked()) { textviewspiel.settext("dabei!"); } else { textviewspiel.settext("nicht dabei :-(."); } switchspiel.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { firstname=firstn.tostring(); spiel_anmel = spiel_anmel1.tostring(); switchspiel(); } }); // training //set switch on //switchtraining.setchecked(true); //attach listener check changes in state switchtraining.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { sharedpreferences.editor editor = getsharedpreferences("switchtraining", mode_private).edit(); editor.putboolean("valueswitchtraining", true); editor.commit(); textviewtraining.settext("am start!!!"); } else { sharedpreferences.editor editor = getsharedpreferences("switchtraining", mode_private).edit(); editor.putboolean("valueswitchtraining", false); editor.commit(); textviewtraining.settext("eine faule sau!"); } } }); //check current state before display screen if (switchtraining.ischecked()) { textviewtraining.settext("am start!!!"); } else { textviewtraining.settext("eine faule sau!"); } } //update mysql spiel (switchspiel()) public void switchspiel() { new downloadwebpagetask().execute(urls, firstname, spiel_anmel); } private class downloadwebpagetask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { //params comes execute call: params[o] url try // initializing parameters { arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("firstname", urls[1])); namevaluepairs.add(new basicnamevaluepair("spiel_anmel", urls[2])); //make http request httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(urls[0]); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); // read response bufferedreader reader = new bufferedreader(new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); // return response return result; } catch (ioexception e) { return "unable retrieve web page. url may invalid."; } } } }
my s.php
$con = mysql_connect($host,$uname,$pwd) or die("connection failed"); mysql_select_db($db,$con) or die("db selection failed"); $name=$_request['firstname']; $training=$_request['spiel_anmel']; $flag['code']=0; if($r = mysql_query("update users set spiel_anmel = '$training' firstname ='$name'", $con)) { $flag['code']=1; } print(json_encode($flag)); mysql_close($con);
i update java code because have implemented 2 buttons sending switch position. org.json.jsonexception: end of input @ character 0 of.
public class abmeldung extends activity { button zurueck; private textview textviewspiel; private textview textviewtraining; private switch switchspiel; private switch switchtraining; //string firstname; string spiel_anmel; inputstream = null; string result = null; string line = null; string username; string sa; int code; private string urlt = "http://.../inserta.php"; private string urls = "http://.../insertb.php"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.abmeldung); textviewspiel = (textview) findviewbyid(r.id.textviewspiel); switchspiel = (switch) findviewbyid(r.id.switchspiel); textviewtraining = (textview) findviewbyid(r.id.textviewtraining); switchtraining = (switch) findviewbyid(r.id.switchtraining); sharedpreferences sharedprefs1 = getsharedpreferences("switchspiel", mode_private); switchspiel.setchecked(sharedprefs1.getboolean("valueswitchspiel", true)); sharedpreferences sharedprefs2 = getsharedpreferences("switchtraining", mode_private); switchtraining.setchecked(sharedprefs2.getboolean("valueswitchtraining", true)); zurueck = (button) findviewbyid(r.id.zurueck); zurueck.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { startactivity(new intent(abmeldung.this, main.class)); } }); /** * import varbiable userid aus sqlite für mysql update **/ databasehandler db = new databasehandler(getapplicationcontext()); /** * hashmap load data sqlite database **/ hashmap<string, string> user = new hashmap<string, string>(); user = db.getuserdetails(); final string uname = user.get("uname"); // spiel //set switch on //switchspiel.setchecked(true); //attach listener check changes in state switchspiel.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { sharedpreferences.editor editor = getsharedpreferences("switchspiel", mode_private).edit(); editor.putboolean("valueswitchspiel", true); editor.commit(); textviewspiel.settext("dabei!"); spiel_anmel = "ja"; } else { sharedpreferences.editor editor = getsharedpreferences("switchspiel", mode_private).edit(); editor.putboolean("valueswitchspiel", false); editor.commit(); textviewspiel.settext("nicht dabei :-(."); spiel_anmel = "nein"; } } }); //check current state before display screen if (switchspiel.ischecked()) { textviewspiel.settext("dabei!"); } else { textviewspiel.settext("nicht dabei :-(."); } // training //set switch on //switchtraining.setchecked(true); //attach listener check changes in state switchtraining.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { sharedpreferences.editor editor = getsharedpreferences("switchtraining", mode_private).edit(); editor.putboolean("valueswitchtraining", true); editor.commit(); textviewtraining.settext("am start!!!"); } else { sharedpreferences.editor editor = getsharedpreferences("switchtraining", mode_private).edit(); editor.putboolean("valueswitchtraining", false); editor.commit(); textviewtraining.settext("eine faule sau!"); } } }); //check current state before display screen if (switchtraining.ischecked()) { textviewtraining.settext("am start!!!"); } else { textviewtraining.settext("eine faule sau!"); } //final string spielan = spiel_anmel; button updates = (button) findviewbyid(r.id.updates); updates.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { sa = spiel_anmel.tostring(); username = uname.tostring(); updates(); } }); } //update mysql spiel (switchspiel()) public void updates() { arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("uname", username)); namevaluepairs.add(new basicnamevaluepair("spiel_anmel", sa)); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(urls); httppost.setentity(new urlencodedformentity(namevaluepairs)); strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); log.e("pass 1", "connection success"); } catch (exception e) { log.e("fail 1", e.tostring()); toast.maketext(getapplicationcontext(), "invalid ip adress", toast.length_long).show(); } try { bufferedreader readers = new bufferedreader( new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sbs = new stringbuilder(); while ((line = readers.readline()) != null) { sbs.append(line + "\n"); } is.close(); result = sbs.tostring(); log.e("pass 2", "connection success"); } catch (exception e) { log.e("fail 2", "error converting result "+ e.tostring()); } try { jsonobject json_datas = new jsonobject(result); code = (json_datas.getint("code")); if (code == 1) { toast.maketext(getbasecontext(), "update successfully", toast.length_short).show(); } else { toast.maketext(getbasecontext(), "sorry, try again", toast.length_short).show(); } } catch (exception e) { log.e("fail 3", e.tostring()); } } }
the corresponding php:
$con = mysql_connect($host,$uname,$pwd) or die("connection failed"); mysql_select_db($db,$con) or die("db selection failed"); $uname=$_request['uname']; $spiel=$_request['spiel_anmel']; $flag['code']=0; if($r=mysql_query("update users set spiel_anmel = 'spiel' uname 'uname'",$con)) { $flag['code']=1; } print(json_encode($flag)); mysql_close($con);
any ideas?
Comments
Post a Comment