3

我能够连接到我的 MySQL 数据库,但问题是。我的 android 没有使用此代码将编辑文本值传递给 PHP。

public class MySQL extends Activity {

    EditText inputName;
     private static String  url_create_product = "http://atlantis-us.com/phpFile.php?";

     JSONParser jsonParser = new JSONParser();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sql);
        inputName = (EditText) findViewById(R.id.txt1);

       ((Button) findViewById(R.id.btnsend)).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new CreateNewProduct().execute();
        }
    });

    }

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

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            String name = inputName.getText().toString();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);

            return null;
        }

    }

}



this is my PHP codes

<?php

    $DB_HostName = "localhost";
    $DB_Name = "_atlantisdb";
    $DB_User = "atlantis_frux";
    $DB_Pass = "frux2012";
    $DB_Table = "testDB";

    if (isset ($_GET["name"]))
        $name = $_GET["name"];
    else
        $name = "Ghalia";

    $con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 
    mysql_select_db($DB_Name,$con) or die(mysql_error()); 

    $sql = "insert into $DB_Table (name) values('$name');";
    $res = mysql_query($sql,$con) or die(mysql_error());

    mysql_close($con);
    if ($res) {
        echo "success";
    }else{
        echo "faild";
    }// end else

?>

任何人都可以追踪我的代码的磨损情况吗?提前致谢。

4

1 回答 1

1

您正在使用POSTandroid 代码中的方法发送编辑文本值,并使用GETphp 代码中的方法获取值。

chage $_GET["name"] to $_POST["name"] or $_REQUEST['name']
于 2012-10-25T07:45:48.180 回答