使用mySQL& Android中的Php

时间:2014-03-24 17:13:17

标签: php android mysql

我试图使用Android应用程序将值添加到我的Mysql数据库中,这是我的Php文件:

<?php 

    $connection = mysql_connect("mywebhost.example", "user", "password")ordie(mysql_error());
    $selection = mysql_select_db("name_of_database", $connection)or die(mysql_error());

    $name = $_POST['name'];
    $points = $_POST['points'];

    $insert = "INSERT INTO my_table('name','points') VALUES('$name','$points')";
    $run = mysql_query($insert)or die(mysql_error());

?>

这是我的Android代码:

public void test_button (View view)
{
    new netsd().execute();

}


class netsd extends AsyncTask<String,String,String> 
{

     @Override
     protected void onPreExecute() {
         super.onPreExecute();
         pDialog = new ProgressDialog(MainActivity.this);
         pDialog.setMessage("Loading . Please wait...");
         pDialog.setIndeterminate(false);
         pDialog.setCancelable(false);
         pDialog.show(); 
     }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("country", country));
        nameValuePairs.add(new BasicNameValuePair("points", Integer.toString(points)));

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://heremyexampel.com/android_connect.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    protected void onPostExecute(String file_url) {


        pDialog.cancel();
    }


}

我在Eclipse中没有遇到任何问题,但是我的Mysql中没有任何值。所以他没有添加它们......我用这个AsyncTask尝试了但是真的不好用......这是什么

  <String,String,String>

是不是因为那个?谢谢 ! (当然,Passwort服务器链接等是示例)

0 个答案:

没有答案