Insert data into MySQL database from Android app. Changes not reflected in database

16,281

Just one point: to get the value from an Android EditText we use

x.getText().toString(); 

where x is an EditText object, right? hope it helps at all.

But, since this is being done in the onCreate() - rather than inside the onClick() - you should change:

nameValuePairs.add(new BasicNameValuePair("sphone", sPhone));

to

nameValuePairs.add(new BasicNameValuePair("sphone", phonenum.getText().toString()));

And similarly for the other one. Cheers.

Share:
16,281
user2201650
Author by

user2201650

Updated on June 04, 2022

Comments

  • user2201650
    user2201650 almost 2 years

    I'm writing an application to insert data into a MySQL database. I wrote the sql query in PHP and when I enter values in the android application, I don't seen any errors in the log cat but the values aren't getting stored in the database.

    Here's the PHP code:

    <?php
    
    $connect = mysqli_connect("localhost","root","","easyassigndroid");
    
    if(mysqli_connect_errno($connect))
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else
    {
        echo "success";
    }
    
    $username = isset($_POST['sPhone']) ? $_POST['sPhone'] : '';
    $password = isset($_POST['sPassword']) ? $_POST['sPassword'] : '';
    
    $query = mysqli_query($connect, "insert into users (sphone, spassword) values ('$username' ,'$password') ");
    
    mysqli_close($connect);
    ?>
    

    Here's the android part:

    protected void onCreate(Bundle savedInstanceState) {
    
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.register_lecturer);
    
        phonenum = (EditText) findViewById(R.id.id_phone);
        password = (EditText) findViewById(R.id.id_password);
    
        sPhone = phonenum.toString();
        sPassword = password.toString();
    
        registerLecturer=(Button)findViewById(R.id.lecturerregister);
        registerLecturer.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                httpclient = new DefaultHttpClient();
                httppost = new HttpPost("http://10.0.2.2/project/insert.php");
    
                try
                {
                    nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("sphone", sPhone));
                    nameValuePairs.add(new BasicNameValuePair("spassword", sPassword));
    
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    response = httpclient.execute(httppost);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    
    
    }
    

    I'm a beginner to both Android and PHP coding. Can anyone tell me where the problem is in my code? I need this for my project work.