我将记录插入mysql表时遇到问题。仅插入最后一条记录。
这是我正在处理的代码:
的Android
String[] arrDocumentNumber, arrTransactionDate, arrItemCode;
arrDocumentNumber = dbHelper.getAllDocumentNumbersFromOUTTRANS();
arrTransactionDate = dbHelper.getAllTransactionDatesFromOUTTRANS();
arrItemCode = dbHelper.getAllItemCodesFromOUTTRANS();
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
for(int i = 0; i < arrDocumentNumber.length; i++){
params.add(new BasicNameValuePair(TAG_DOCUMENTNUMBER, arrDocumentNumber[i] ));
params.add(new BasicNameValuePair(TAG_TRANSACTIONDATE, arrTransactionDate[i] ));
params.add(new BasicNameValuePair(TAG_ITEMCODE, arrItemCode[i] ));
}
// getting JSON Object
// Note that insert outtrans url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_insertTo_outtrans,
"POST", params);
//obj.put(params);
//Log.d("JSON", obj.toString());
// check log cat from response
Log.d("Create Response", json.toString());
}
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['documentnumber']) && isset($_POST['transactiondate']) && isset($_POST['itemcode']) ){
$documentnumber = $_POST['documentnumber'];
$transactiondate = $_POST['transactiondate'];
$locationdate = $_POST['locationdate'];
$itemcode = $_POST['itemcode'];
$qty = $_POST['qty'];
$amount = $_POST['amount'];
$reson = $_POST['reason'];
$posted = $_POST['posted'];
$dateposted = $_POST['dateposted'];
$unitprice = $_POST['unitprice'];
// include db connect class
include('dbconnect.php');
$result = $conn->prepare("INSERT INTO outtrans(documentnumber, transactiondate, itemcode) VALUES (:documentnumber, :transactiondate, :itemcode)");
$result->bindParam(':documentnumber', $documentnumber);
$result->bindParam(':transactiondate', $transactiondate);
$result->bindParam(':itemcode', $itemcode);
$result->execute();
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "New record 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);
}
?>
只有最后一条记录被插入到mysql表中。我对PHP知之甚少,所以我可能在PHP代码中遗漏了一些东西。我将非常感谢您的帮助。感谢。
答案 0 :(得分:3)
这是因为您只使用最后一个参数进行HttpRequest
。 (即)完成for循环后,params
将仅包含最后一条记录。
所以移动
JSONObject json=jsonParser.makeHttpRequest(url_insertTo_outtrans,"POST",params);
进入for循环。
for(int i = 0; i < arrDocumentNumber.length; i++)
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(TAG_DOCUMENTNUMBER, arrDocumentNumber[i] ));
params.add(new BasicNameValuePair(TAG_TRANSACTIONDATE, arrTransactionDate[i] ));
params.add(new BasicNameValuePair(TAG_ITEMCODE, arrItemCode[i] ));
JSONObject json=jsonParser.makeHttpRequest(url_insertTo_outtrans,"POST",params);
}