将编辑文本数据从android传递给php

时间:2014-07-25 05:53:54

标签: php android

我需要从包含数百个项目的外部数据库中读取数据。到目前为止我所做的是我写了一个返回所有项目的php查询,所以我已经完成了

PHP:

$db_host  = "host";
$db_uid  = "username";
$db_pass = "password";
$db_name  = "person"; 
$db_con = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect');
mysql_select_db($db_name);
$sql = "SELECT * FROM employee ";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
mysql_close();   

机器人:

String url = "http://localhost/index.php";

@Override
public void onCreate(Bundle savedInstanceState) {

    /*
     * StrictMode is most commonly used to catch accidental disk or network
     * access on the application's main thread
     */

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads().detectDiskWrites().detectNetwork()
            .penaltyLog().build());

    super.onCreate(savedInstanceState);
    setContentView(R.layout.hospital);

    byear = (EditText) findViewById(R.id.editText1);
    submit = (Button) findViewById(R.id.submitbutton);
    tv = (TextView) findViewById(R.id.showresult);

    // define the action when user clicks on submit button
    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // declare parameters that are passed to PHP script i.e. the
            // name "birthyear" and its value submitted by user
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            data = byear.getText().toString();
            // define the parameter
            postParameters.add(new BasicNameValuePair("data", data));
            String response = null;

            // call executeHttpPost method passing necessary parameters
            try {

                response = CustomHttpClient.executeHttpPost(

                url,
                postParameters);

                // store the result returned by PHP script that runs
                // MySQL query
                String result = response.toString();

                // parse json data
                try {
                    returnString = "";
                    JSONArray jArray = new JSONArray(result);
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag", "id: " + json_data.getInt("id")
                                + ", name: " + json_data.getString("name")

                        );
                        // Get an output to the screen


                        returnString += "\n" + "Name ="
                                + json_data.getString("name") + "\n"
                                + "Contact number = "
                                + json_data.getInt("contact") + "\n"

                    }
                } catch (JSONException e) {
                    Log.e("log_tag", "Error parsing data " + e.toString());
                }

                try {
                    tv.setText(returnString);
                } catch (Exception e) {
                    Log.e("log_tag", "Error in Display!" + e.toString());
                    ;
                }
            } catch (Exception e) {
                Log.e("log_tag",
                        "Error in http connection!!" + e.toString());
            }
        }
    });
}

}

我的问题是如何将数据从编辑文本android传递给php查询,以便查询只返回正确的项目

3 个答案:

答案 0 :(得分:1)

使用postParameters将数据发送到php:

postParameters.add(new BasicNameValuePairs("id",data));

在你的php脚本中添加

if(isset($_POST['id']){
    var id = $_POST['id'];
    // do db operation here
}

id =您的数据ID和数据=您的数据。

答案 1 :(得分:0)

$var_data=$_REQUEST['data'];
    print($var_data);

在第1行的PHP代码中使用2行以上,这样就会打印与关键字&#34;数据&#34;相关的android数据。

 postParameters.add(new BasicNameValuePair("data", data));

答案 2 :(得分:0)

首先你必须编写相应的PHP脚本然后你可以使用它:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://Your Url");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("uid",username));
nameValuePairs.add(new BasicNameValuePair("filename",filename));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

确保服务器端表中的字段名称分别为“uid”和“filename”。 有关详细信息,请参阅this