2

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.

4
  • I think you are getting exception so that why its not insert data in database. Debug your application. Commented Mar 31, 2013 at 11:27
  • Is the PHP code correct? And how about the Android code? Commented Mar 31, 2013 at 11:29
  • PHP code looks OK (as far as names and DB entries are corect). I would recomend try it first with $_GET, because you can easily check if PHP code is correct. Commented Mar 31, 2013 at 11:36
  • I still can't see any changes reflected in the database :'( Commented Mar 31, 2013 at 11:55

2 Answers 2

2

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.

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

4 Comments

I put the .getText() method. But now, a space(' ') is getting stored in the DB when i enter '1234512345' as the value for the sPhone EditText field in the emulator. Wonder why this is happening. This has been troubling me for the past 2-3 days. :'(
You should do this: put the x.getText().toString() - in the onClick() function - not in the onCreate() - as the values will only be those that they contained at the time of creation. Cheers.
No worries, buddy! I'm glad. May I trouble you to tick the answer as correct (or at least up vote it) - this way people who come here know what to do.
Done :) I just ticked it :)
0

As I can see, you ar using "sPhone" and "sphone" as keys. I would recommend to have it both with same names.

nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("sphone", sPhone));
nameValuePairs.add(new BasicNameValuePair("spassword", sPassword));

And in PHP

$_POST['sphone']
$_POST['spassword']

8 Comments

I'm getting an "Undefined index :sphone" and "Undefined index :spassword" when i try your PHP code.
You can´t get undefined index when you are using isset()... You must have something else wrong. Have you change all keys ?
I put the isset(). No errors in the PHP code. But the 'sphone' field in the database is getting stored as 'andorid.wi' when i enter sphone field in the emulator as '1234512345'.
I dont understand your comment. But if your DB has sphone field as number and you try to store String in that, no data is changed. PHP that can return error, but you must do it for yourself and obtain it from mysqli (at least, I think)
I've put the sphone field in the DB as varchar. I'm entering '1234512345' in textfield in the emulator. But it isn't getting stored as '1234512345' in the DB. It is getting stored as 'android.wi' instead. I hope I'm clear? :)
|

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.