简体   繁体   English

php mysql android中多次插入错误

[英]multiple inserts error in php mysql android

i have an android application that should insert an entry in a table, however to achieve that two inserts should be done first in the tables that have the foreign keys of the corresponding table. 我有一个android应用程序,应该在表中插入一个条目,但是要实现具有相应表的外键的表应该先进行两次插入。 if i insert the entries manually on the phpmyadmin (each table by itself) everything works just fine, so the error must be in the php script below: 如果我在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);
  }
      ?>

so the json response is oops an error occured ... none of the inserts is being done 所以json响应糟糕了发生了一个错误...没有任何插入完成

The Error is : Value of type java.lang.String cannot be converted to JSONObject 错误是:无法将类型为java.lang.String的值转换为JSONObject

here's the Java code: 这是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;

    }

You should connect to the server first: 您应该首先连接到服务器:

http://php.net/manual/en/function.mysql-connect.php http://php.net/manual/zh/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) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM