字符串无法转换为json对象

时间:2013-06-13 08:52:37

标签: php android

我试图从表中获取列值并将该值插入另一个表。 但我收到错误:字符串无法转换为json对象。

请帮忙。

这是我的php代码:

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['ailment']) && isset($_POST['medicine_name']) && isset($_POST['comment']) && isset($_POST['prescription_id']) && isset($_POST['patient_id'])  
&& isset($_POST['qty1']) && isset($_POST['qty2']) && isset($_POST['qty3'])) {

    $ailment = $_POST['ailment'];
    $prescription_id = $_POST['prescription_id'];
    $comment = $_POST['comment'];
$patient_id = $_POST['patient_id'];
$medicine_name = $_POST['medicine_name'];
$qty1 = $_POST['qty1'];
$qty2 = $_POST['qty2'];
$qty3 = $_POST['qty3'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO prescription(prescription_id,ailment,patient_id,comment) VALUES('$prescription_id',
'$ailment','$patient_id','$comment')");
   $result2 = mysql_query("select medicine_id from medicine where medicine_name=$medicine_name");

   $medical = mysql_fetch_row($result2);
//$medical = $med['medicine_id'];
$med = $medical[0];   
mysql_query("INSERT INTO dosage values('$prescription_id','$med','$qty1','$qty2','$qty3')");


    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

以下是活动文件的异步任务。

private class AddPrescriptionDetails extends AsyncTask<String, Void, String> {
        //  JSONObject product;

        protected String doInBackground(String... args) {

            //JSONObject product = null;
            //id.setText(100);
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
               public void run() {
                    // Check for success tag

                        String ail = ailment.getText().toString();
                       String med = medicine.getText().toString();
                       String comm = comment.getText().toString();
                       String pre = presc.getText().toString();
                       String do1 = dosage1.getText().toString();
                       String do2 = dosage2.getText().toString();
                       String do3 = dosage3.getText().toString();
                       // Building Parameters
                       List<NameValuePair> params = new ArrayList<NameValuePair>();
                       params.add(new BasicNameValuePair("ailment", ail));
                       params.add(new BasicNameValuePair("medicine_name", med));
                       params.add(new BasicNameValuePair("comment", comm));
                       params.add(new BasicNameValuePair("prescription_id", pre));
                       params.add(new BasicNameValuePair("patient_id", pid));
                       params.add(new BasicNameValuePair("qty1", do1));
                       params.add(new BasicNameValuePair("qty2", do2));
                       params.add(new BasicNameValuePair("qty3", do3));
                       // getting JSON Object
                       // Note that create product url accepts POST method
                       JSONObject json = jsonParser.makeHttpRequest(url_app_presc,
                               "POST", params);
                        // json success tag
                       Log.d("Create Response", json.toString());

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

                           if (success == 1) {
                               // successfully created product
                               Intent i = new Intent(AddPresc.this, DocPresc.class);
                               startActivity(i);

                               // closing this screen
                               finish();
                           } else {
                               // failed to create product
                           }
                       } catch (JSONException e) {
                           e.printStackTrace();
                       }
         //      }
               }

            });
                    //return product;
             return null;

          //  });

           // return product;
        }

    }

0 个答案:

没有答案