2014-10-09 58 views
0

我要創建一個簡單的登記表格應用程序,當用戶插入他們的用戶名和密碼的EditText在UI顯示其數據庫記錄的IDAndroid的登記表與PHP和MySQL

我的應用程序正確插入用戶到數據庫中;只是我的EditText沒有顯示ID ...

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#00aeef" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="50dp" 
    android:layout_marginTop="18dp" 
    android:text="Register Example" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textColor="#ffffff" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/textView3" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="45dp" 
    android:text="Username:" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textColor="#ffffff" /> 

<EditText 
    android:id="@+id/edt_username" 
    android:layout_width="250dp" 
    android:layout_height="40dp" 
    android:layout_below="@+id/textView3" 
    android:layout_centerHorizontal="true" 
    android:background="#ffffff" 
    android:ems="10" 
    android:padding="5dp" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/btn_insert" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/edt_result" 
    android:layout_below="@+id/edt_password" 
    android:layout_marginTop="16dp" 
    android:background="#ffffff" 
    android:text="Insert" 
    android:textColor="#00aeef" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_below="@+id/edt_username" 
    android:text="Password:" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textColor="#ffffff" /> 

<EditText 
    android:id="@+id/edt_password" 
    android:layout_width="250dp" 
    android:layout_height="40dp" 
    android:layout_alignLeft="@+id/edt_username" 
    android:layout_below="@+id/textView2" 
    android:background="#ffffff" 
    android:ems="10" 
    android:padding="5dp" /> 

<EditText 
    android:id="@+id/edt_result" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:layout_below="@+id/btn_insert" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="22dp" 
    android:background="#ffffff" 
    android:ems="10" 
    android:padding="5dp" /> 

</RelativeLayout> 

InsertClass.java

package com.example.testphp; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask; 

public class InsertClass extends AsyncTask<String, Void, String>{ 

private Context context; 
private ProgressDialog pDialog; 
private EditText edt; 
private String json = ""; 
private JSONObject jObj = null; 

public InsertClass(Context context, EditText edt) 
{ 
    this.context = context; 
    pDialog = new ProgressDialog(context); 
    this.edt = edt; 
} 

@Override 
protected void onPreExecute() { 
    pDialog.setMessage("Loading... Please wait"); 
    pDialog.show(); 
    super.onPreExecute(); 
} 

@Override 
protected String doInBackground(String... arg0) { 

    try 
    { 
    String username = (String)arg0[0]; 
    String password = (String)arg0[1]; 
    String link = "http://10.0.2.2:8020/test/test.php"; 
    String data = URLEncoder.encode("username","utf-8") + 
    "=" + URLEncoder.encode(username,"utf-8"); 
    data += "&" + URLEncoder.encode("password","utf-8") + 
      "=" + URLEncoder.encode(password,"utf-8"); 
    URL url = new URL(link); 
    URLConnection conn = url.openConnection(); 
    conn.setDoOutput(true); 
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
    wr.write(data); 
    wr.flush(); 
    BufferedReader reader = new BufferedReader 
      (new InputStreamReader(conn.getInputStream())); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while((line = reader.readLine()) != null) 
    { 
     sb.append(line); 
     break; 
    } 
    json = sb.toString(); 
    jObj = new JSONObject(json); 
    edt.setText(jObj.getString("id")); 
    return sb.toString(); 
    } 
    catch(JSONExeption e) 
    { 
     return new String("Exeption: " + e.getMessage()); 
    } 
    catch(Exception e) 
    { 
     return new String("Exeption: " + e.getMessage()); 
    } 
} 

@Override 
protected void onPostExecute(String result) { 
    pDialog.dismiss(); 
    super.onPostExecute(result); 
} 
} 

MainActivity.java

package com.example.testphp; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends Activity { 

String username; 
String password; 
EditText edtUsername; 
EditText edtPassword; 
EditText edtResult; 
Button btnInsert; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btnInsert = (Button) findViewById(R.id.btn_insert); 
    edtPassword = (EditText) findViewById(R.id.edt_password); 
    edtUsername = (EditText) findViewById(R.id.edt_username); 
    edtResult = (EditText) findViewById(R.id.edt_result); 

    btnInsert.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      username = edtUsername.getText().toString(); 
      password = edtPassword.getText().toString(); 
      new InsertClass(MainActivity.this, edtResult).execute(username,password); 

     } 
    }); 

} 

    @Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

test.php的

<?php 

$con = mysqli_connect("localhost" , "root" , "","test"); 
if(mysqli_connect_errno($con)) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_errno(); 
} 
$username = $_POST['username']; 
$password = $_POST['password']; 

$result = mysqli_query($con,"INSERT INTO test (username,pass) VALUES '$username','$password')"); 
if($result) 
{ 
$id_result = mysqli_query($con,"SELECT id FROM test WHERE username = $username"); 
if($id_result) 
{ 
$response = array("id" => $id_result); 
echo json_encode($response); 
} 
} 
mysqli_close($con); 
?> 

任何建議,將不勝感激......

+0

你聽說過關於** SQL注射**?這將是第一個明顯的建議。 – shadyyx 2014-10-09 12:08:26

+0

你捕獲異常,並用它做什麼(至少做'e.printStackTrace()'看到它在logcat中)...我敢肯定,你正在傷害UI與壞的觸摸,用戶界面不會喜歡從不是UI線程 – Selvin 2014-10-09 12:17:28

+0

@shadyyx,還有,我從來沒有聽說過SQL注入!坦克爲你的提示。通過這種方式,首先,我將打包我的應用程序結構,然後我將添加安全策略... – 2014-10-09 14:06:06

回答

1

據我所看到的,你有你的SQL 2個錯誤。

此行缺少一個支架:

$result = mysqli_query($con,"INSERT INTO test (username,pass) VALUES '$username','$password')"); 
                    ^bracket 

其中應閱讀:

$username

$id_result = mysqli_query($con,"SELECT id FROM test WHERE username = $username"); 

$result = mysqli_query($con,"INSERT INTO test (username,pass) VALUES ('$username','$password')"); 

此行缺少引號括應閱讀:

$id_result = mysqli_query($con,"SELECT id FROM test WHERE username = '$username'"); 

or die(mysqli_error($con))添加到mysqli_query()以查看實際錯誤。

你也應該防範SQL注入,改變你的POST變量:

$username = stripslashes($_POST['username']); 
$username = mysqli_real_escape_string($con, $_POST['username']); 

$password = stripslashes($_POST['password']); 
$password = mysqli_real_escape_string($con, $_POST['password']); 

查找到mysqli with prepared statements,或PDO with prepared statements他們更安全

我不能幫你了Android的代碼,但看看Selvin在評論中提到:

「你捕獲異常,什麼也不做它(至少做e.printStackTrace()在Logcat中看到它)...我敢肯定,你是用壞的觸摸傷害UI,UI不喜歡被觸摸,而不是UI線程「

+0

坦克爲您的提示。我檢查了我的select語句,並且我已經失去** $ username **的引用。另外,我補充或**模具(mysqli_query($ CON)**我陳述現在,我的EditText fillout與此消息: – 2014-10-09 14:15:37

+0

{ 「lenghts:」 空 「NUM_ROWS:」 空 「鍵入:」 空」 current_field:「空」,FIELD_COUNT:「空} --------你知道是什麼意思 – 2014-10-09 14:19:11

+0

不太清楚,但如果這是你得到了實際的代碼,字'lenghts'是拼寫錯誤,並應注意什麼?!閱讀爲「長度」@FarshadKazemi是你的代碼中某處的「lenghts」部分嗎? – 2014-10-09 14:22:23