尝试使用Android和PHP插入MYSQL时出错

时间:2012-06-19 10:32:19

标签: php android mysql

我正在尝试使用android模拟器3.1插入到mysql中,但我的问题我无法插入,它给我在logcat中的错误可以有人帮助我请在这里我的代码。有趣的部分它插入空数据到我的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);

我的活动

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

    }

我试图谷歌错误,但没有找到它。我只需要知道该怎么做,因为它差不多3个小时尝试此代码。我的日志猫错误

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

1 个答案:

答案 0 :(得分:1)

您需要更改此

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

它必须和像这样的PHP一样

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