gpt4 book ai didi

php - php mysql android 中的多次插入错误

转载 作者:行者123 更新时间:2023-11-29 22:27:58 26 4
gpt4 key购买 nike

我有一个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 响应是哎呀发生了错误...没有任何插入被完成

错误是: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;

}

最佳答案

您应该首先连接到服务器:

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) {

关于php - php mysql android 中的多次插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30085400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com