php mysql android中的多个插入错误

时间:2015-05-06 19:01:09

标签: php mysql

我有一个应该在表中插入一个条目的android应用程序,但要实现这两个插入应该首先在具有相应表的外键的表中完成。 如果我在phpmyadmin上手动插入条目(每个表本身)一切正常,所以错误必须在下面的PHP脚本中:

   <?php



$response = array();

     // check for required fields
      if (isset($_POST['UserName']) && isset($_POST['Password']) && isset($_POST['BankName']) && isset($_POST['TransId']) && isset($_POST['Name'])
 && isset($_POST['City']) && isset($_POST['Region']) && isset($_POST['Gender']) && isset($_POST['Email']) && isset($_POST['Phone'])
           ) {

$UserName = $_POST['UserName'];
$Password = $_POST['Password'];
$BankName = $_POST['BankName'];
$TransId = $_POST['TransId'];
$Name = $_POST['Name'];
$City = $_POST['City'];
$Region = $_POST['Region'];
$Gender = $_POST['Gender'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone'];

$query1 = "INSERT INTO location(`GeoPosition`, `City`, `Region`) VALUES (NULL,'$City','$Region')";
$result1 = mysql_query($query1);
if ($result1) {

    $query2 = "Insert into bank(BankName, BankId , TransId , moneyReceived , CreditCardId ,CustomerName)
                Values ('$BankName',NULL,'$TransId',1,NULL,'$Name')";
    $result2 = mysql_query($query2);
    if($result2){

        // mysql inserting a new row
        $query3= "INSERT INTO premiumseeker(UserName, Password, BankName,TransId, uploadCV ,Name, CreditCardId, Email,City,Region,Gender,Phone)
                            VALUES('$UserName', '$Password', '$BankName','$TransId', NULL,'$Name',NULL,'$Email', '$City', '$Region','$Gender','$Phone')";
        $result3 = mysql_query($query3);                            

        // check if row inserted or not
        if($result3){

            // successfully inserted into database
            $response["success"] = 1;
            $response["message"] = "Premium Seeker successfully registered.";

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

    // 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);
  }
      ?>

所以json响应是oops发生错误...没有插件正在完成

错误是: java.lang.String类型的值无法转换为JSONObject

这里是Java代码:

     int success;
        String Name = etPremiumSeeker.getText().toString();
        String City = etPseekerCity.getText().toString();
        String Region = spinnerRegion.getSelectedItem().toString();
        String BankName = etBankPSeeker .getText().toString();
        String Email = etPSeekerMail.getText().toString();
        String Phone = etPSeekerPhone.getText().toString();
        String UserName = etPSeekerUser.getText().toString();
        String Password = etPSeekerPass.getText().toString();
        String TransId = "1245";

        try {

           /* try {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(password.getBytes(),0,password.length());
                password = new BigInteger(1,md5.digest()).toString(16);
                //System.out.println("Signature: "+signature);

            } catch (final NoSuchAlgorithmException e) {
                e.printStackTrace();
            }*/

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("UserName", UserName));
            params.add(new BasicNameValuePair("Password", Password));
            params.add(new BasicNameValuePair("BankName", BankName));
            params.add(new BasicNameValuePair("TransId", TransId));
            params.add(new BasicNameValuePair("Name", Name));
            params.add(new BasicNameValuePair("City", City));
            params.add(new BasicNameValuePair("Region", Region));
            params.add(new BasicNameValuePair("Gender", Gender));
            params.add(new BasicNameValuePair("Email", Email));
            params.add(new BasicNameValuePair("Phone", Phone));


            // Log.d("request!", "starting");

            //Posting user data to script
            JSONObject json2 = jsonParser2.makeHttpRequest(
                    REGISTER_URL, "POST", params);

            // full json response
             Log.d("Register attempt", json2.toString());

            // json success element
            success = json2.getInt(TAG_SUCCESS);
            if (success == 1) {
                    Log.d("User Created!", json2.toString());
                //finish();
                return json2.getString(TAG_MESSAGE);
            } else {
                    Log.d("Registration Failure!", json2.getString(TAG_MESSAGE));
                    return json2.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

1 个答案:

答案 0 :(得分:0)

您应首先连接到服务器:

http://php.net/manual/en/function.mysql-connect.php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$db_selected = mysql_select_db('foo', $link);

$query1 = "INSERT INTO location(`GeoPosition`, `City`, `Region`) VALUES (NULL,'$City','$Region')";
$result1 = mysql_query($query1);
if ($result1) {