Unable to send value of variable from android to php -
i want send username edittext php code inserts table.so far code unable so.code below:
loginstudent.java
public class loginstudent extends activity { button b1; edittext e1, username;@ override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.loginstudent); b1 = (button) findviewbyid(r.id.button1); e1 = (edittext) findviewbyid(r.id.edittext1); username = (edittext) findviewbyid(r.id.phone_no); b1.setonclicklistener(new view.onclicklistener() {@ override public void onclick(view arg0) { // todo auto-generated method stub if (e1.gettext().length() == 0 || username.gettext().length() == 0) { toast.maketext(getbasecontext(), "please enter fields!!", toast.length_long).show(); } else { //toast.maketext(getbasecontext(), "registered successfully!", toast.length_long).show(); intent = new intent(loginstudent.this, verify.class); startactivity(i); } } }); } public void login(view view) { string username1 = username.gettext().tostring(); new signinactivity(this).execute(username1); } }
signinactivity.java
public class signinactivity extends asynctask < string, void, string > { private context context; public signinactivity(context context) { this.context = context; } protected void onpreexecute() { } @override protected string doinbackground(string...arg0) { try { string username = (string) arg0[0]; string link = "http://example.com/try1.php?username=" + username; url url = new url(link); httpclient client = new defaulthttpclient(); httpget request = new httpget(); request.seturi(new uri(link)); httpresponse response = client.execute(request); bufferedreader in = new bufferedreader(new inputstreamreader(response.getentity().getcontent())); stringbuffer sb = new stringbuffer(""); string line = ""; while ((line = in .readline()) != null) { sb.append(line); break; } in .close(); return sb.tostring(); } catch (exception e) { return new string("exception: " + e.getmessage()); } } @suppresslint("newapi")@ override protected void onpostexecute(string result) { if (result.isempty()) { system.out.println("user not present"); } else { system.out.println(result); } } }
try1.php
<?php class pswd { function generatepassword($length, $strength) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; if ($strength & 1) { $consonants .= 'bdghjlmnpqrstvwxz'; } if ($strength & 2) { $vowels .= "aeuy"; } if ($strength & 4) { $consonants .= '23456789'; } if ($strength & 8) { $consonants .= '@#$%'; } $password = ''; $alt = time() % 2; ($i = 0; $i < $length; $i++) { if ($alt == 1) { $password .= $consonants[(rand() % strlen($consonants))]; $alt = 0; } else { $password .= $vowels[(rand() % strlen($vowels))]; $alt = 1; } } return $password; } } $con = mysqli_connect("example.com", "abc", "1234", "pqrs"); if (mysqli_connect_errno($con)) { echo "failed connect mysql: " . mysqli_connect_error(); } $username = (int) $_get['username']; echo $username; $result = mysqli_query($con, "select * login_stud phone_no = '$username'"); if ($row = mysqli_fetch_array($result)) { echo " user present"; } else { $p = new pswd(); $passwd = $p->generatepassword(4, 0); $result = mysqli_query($con, "insert login_stud (phone_no,password)values ('$username','$passwd')"); } mysqli_close($con); ?>
<?php $con = mysqli_connect("example.com", "abc", "1234", "pqrs"); if (mysqli_connect_errno($con)) { echo "failed connect mysql: " . mysqli_connect_error(); } //$username = (int) $_get['username']; $username = $_get['username']; echo $username; $result = mysqli_query($con, "select * login_stud phone_no = '$username'"); if ($row = mysqli_fetch_array($result)) { echo " user present"; } else { $p = new pswd(); $passwd = $p->generatepassword(4, 0); $result = mysqli_query($con, "insert login_stud (phone_no,password)values ('$username','$passwd')"); } mysqli_close($con); ?>
you sending username in string type when retrieve int type. value is
0.
Comments
Post a Comment