0

I'm trying to insert into mysql using android emulator 3.1,but my problem I cant able to insert, it giving me errors in logcat can someone help me please here my code.The funny part it inserting empty data into my mysql.

PHP

mysql_select_db("properties", $con);

$sql="INSERT INTO customer(C_Name, C_Surname, C_ID, C_Address, C_Email, C_CellNumber, C_UserName, C_Password)
   VALUES('$_POST[C_Name]','$_POST[C_Surname]','$_POST[C_ID]','$_POST[C_Address]','$_POST[C_Email]',
   '$_POST[C_CellNumber]','$_POST[C_UserName]','$_POST[C_Password]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
//echo "1 record added";

echo json_encode($sql);

mysql_close($con);

And my Activity

protected String doInBackground(String... args) {

            String name = input_Name.getText().toString();
            String surname = input_Surname.getText().toString(); 
            String ID = input_ID.getText().toString();        
            String Address = input_Address.getText().toString();
            String Emial = input_Email.getText().toString();
            String CellNumber = input_CellNumber.getText().toString();
            String Username = input_UserName.getText().toString();
            String Pasword = input_Password.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name",name)); 
            params.add(new BasicNameValuePair("surname",surname));
            params.add(new BasicNameValuePair("ID",ID));        
            params.add(new BasicNameValuePair("Address",Address));
            params.add(new BasicNameValuePair("Emial",Emial));
            params.add(new BasicNameValuePair("CellNumber",CellNumber));
            params.add(new BasicNameValuePair("Username",Username));
            params.add(new BasicNameValuePair("Pasword",Pasword));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);



            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product

                    Toast.makeText(RegisterUsers.this,"Customer has been Registered",Toast.LENGTH_LONG).show();        

                    // closing this screen
                   // finish();
                } else {
                    // failed to create product

                    Toast.makeText(RegisterUsers.this,"Error",Toast.LENGTH_LONG).show();   
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }

I tried to google the error but did not find it.I just need to know what to do because it almost 3 hours trying this code.My log cat error

LOGCAT

06-18 22:46:45.045: E/JSON Parser(1453): Error parsing data org.json.JSONException: Value INSERT INTO customer(C_Name, C_Surname, C_ID, C_Address, C_Email, C_CellNumber, C_UserName, C_Password)

06-18 22:46:45.045: E/JSON Parser(1453):    VALUES('','','','','',

06-18 22:46:45.045: E/JSON Parser(1453):    '','','') of type java.lang.String cannot be converted to JSONObject
06-18 22:46:45.065: W/dalvikvm(1453): threadid=9: thread exiting with uncaught exception (group=0x40014760)
06-18 22:46:45.095: E/AndroidRuntime(1453): FATAL EXCEPTION: AsyncTask #1
06-18 22:46:45.095: E/AndroidRuntime(1453): java.lang.RuntimeException: An error occured while executing doInBackground()
06-18 22:46:45.095: E/AndroidRuntime(1453):     at android.os.AsyncTask$3.done(AsyncTask.java:266)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at java.lang.Thread.run(Thread.java:1020)
06-18 22:46:45.095: E/AndroidRuntime(1453): Caused by: java.lang.NullPointerException
06-18 22:46:45.095: E/AndroidRuntime(1453):     at de.vogella.android.locationapi.simple.RegisterUsers$CreateNewCustomer.doInBackground(RegisterUsers.java:136)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at de.vogella.android.locationapi.simple.RegisterUsers$CreateNewCustomer.doInBackground(RegisterUsers.java:1)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at android.os.AsyncTask$2.call(AsyncTask.java:252)
06-18 22:46:45.095: E/AndroidRuntime(1453):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-18 22:46:45.095: E/AndroidRuntime(1453):     ... 4 more
4
  • which is line number 136 in you doInBackground method? Commented Jun 19, 2012 at 10:36
  • Here is my guess. On this line Log.d("Create Response", json.toString()); you are trying to convert json to string. And logcat says json cannot be converted into a string. Commented Jun 19, 2012 at 10:38
  • there is no code in that line Commented Jun 19, 2012 at 10:38
  • owk! I get you but I cant fix it, can you help me? Commented Jun 19, 2012 at 14:06

1 Answer 1

1

You need change this

params.add(new BasicNameValuePair("name",name)); 
params.add(new BasicNameValuePair("surname",surname));

And It must be the same as in php like this

params.add(new BasicNameValuePair("C_Name",name)); 
params.add(new BasicNameValuePair("C_Surname",surname));
Sign up to request clarification or add additional context in comments.

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.