0
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();
                }

This is my phpcode

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

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


                ?>

Maybe my php code have a problem I couldn't find what the problem is.This is my Php and java code I am unable to insert data although it's connected to the database. I can't figure it out what mistake I have done.

2 Answers 2

3

Your parameters for mysql_query are in the wrong order.

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

The link identifier / connection should be added via the second parameter, whereas the query should be added via the first parameter.

Change:

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

to

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

Also, your code is open to SQL injection as you're inserting external data directly into your query. Use prepared statements with PDO or at least use mysql_real_escape_string if that is not an option.

Sign up to request clarification or add additional context in comments.

Comments

0

The database link isn't really required. If passed however, it is only passed at the end of the mysql_query statement. i.e:

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

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.