无法用android将数据插入mysql数据库

时间:2013-06-14 11:40:58

标签: php android

EditText tv_username;
    EditText tv_firstname;
    EditText tv_age;
    Button reg;

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

        tv_username = (EditText) findViewById(R.id.username);
        tv_firstname = (EditText) findViewById(R.id.firstname);
        tv_age = (EditText) findViewById(R.id.age);
        reg = (Button) findViewById(R.id.register);



        reg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/regandroid.php");
                if(httppost != null)
                {
                    Context context = getApplicationContext();
                    CharSequence text = "Connected";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();

                }
                try
                {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("username", tv_username.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("firstname", tv_firstname.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("age", tv_age.getText().toString()));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);

                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

这是我的phpcode
                     

            $username = $_POST['username'];
             $firstname = $_POST['firstname'];
            $age = $_POST['age'];

           $query = mysql_query($connect, "insert into users
               (username, firstname,age) values             ('$username' 
                ,'$firstname','$age') ");


                ?>

也许我的PHP代码有问题我找不到问题所在。这是我的Php和java代码我无法插入数据虽然它连接到数据库。我无法弄清楚我做了什么错误。

2 个答案:

答案 0 :(得分:3)

mysql_query的参数顺序错误。

  

mysql_query(string $ query [,resource $ link_identifier = NULL])

应通过第二个参数添加链接标识符/连接,而应通过第一个参数添加查询。

变化:

$query = mysql_query($connect, "insert into users(username, firstname,age) values ('$username', '$firstname', '$age')");

$query = mysql_query("insert into users(username, firstname,age) values ('$username', '$firstname', '$age')", $connect);

此外,当您将外部数据直接插入查询时,您的代码将对SQL注入开放。将准备好的语句与PDO一起使用,或者如果不是选项,至少使用mysql_real_escape_string

答案 1 :(得分:0)

实际上并不需要数据库链接。但是,如果传递它,它只在mysql_query语句的末尾传递。即:

$query = mysql_query("insert into users (username, firstname, age) values ('$username', '$firstname', '$age')", $connect);

希望这有帮助!