2014-04-14 74 views
0

我想從發送的EditText名的PHP代碼,將其插入到一個table.So到目前爲止我的代碼是無法做到so.code低於:無法從Android發送變量的值到PHP

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 all the fields!!", Toast.LENGTH_LONG).show(); 
       } else { 
        //Toast.makeText(getBaseContext(), "Registered successfully!", Toast.LENGTH_LONG).show(); 
        Intent i = 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; 
     for ($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 to connect to MySQL: " . mysqli_connect_error(); 
} 
$username = (int) $_GET['username']; 
echo $username; 
$result = mysqli_query($con, "SELECT * from login_stud where 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 into login_stud (phone_no,password)values  ('$username','$passwd')"); 
} 
mysqli_close($con); 
?> 
+0

你不是在說什麼會發生,提供更多信息。但有一件事讓我感到震驚,它不應該是'HttpPost'而不是'HttpGet'嗎? –

+0

我想發送用戶名從Android到PHP代碼和PHP代碼將插入用戶名到表中。我能夠從php代碼中插入用戶名到表中,但是如果我將值從android傳遞給php,然後將php傳遞給表,我無法做到這一點 – user3523322

+0

沒有太多的PHP人,但我記得朋友有同樣的問題,請確保以UTF-8格式發送數據並將其作爲UTF-8 –

回答

0
<?php 
$con = mysqli_connect("example.com", "abc", "1234", "pqrs"); 
if (mysqli_connect_errno($con)) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
//$username = (int) $_GET['username']; 
$username = $_GET['username']; 
echo $username; 
$result = mysqli_query($con, "SELECT * from login_stud where 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 into login_stud (phone_no,password)values  ('$username','$passwd')"); 
} 
mysqli_close($con); 
?> 

您發送的字符串類型的用戶名,但是當你檢索此爲int類型。所以它的值是
總是0.

+0

發送給// // username =(int)$ _GET ['username']; to $ username = $ _GET ['username'];仍然無法正常工作 – user3523322

+0

首先檢查數據庫連接是否正常? – Hanif

+0

連接正常 – user3523322