android/php 记录未插入 mysql
Posted
技术标签:
【中文标题】android/php 记录未插入 mysql【英文标题】:android/php record not inserting into mysql 【发布时间】:2013-12-31 23:17:17 【问题描述】:我在将记录插入 mysql 表时遇到问题。只插入最后一条记录。
这是我正在处理的代码:
安卓
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
/*
* 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 代码中可能会遗漏一些东西。我将不胜感激您的帮助。谢谢。
【问题讨论】:
能否请您发布 JSON 解析器的代码 【参考方案1】:这是因为您只使用最后一个参数制作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);
【讨论】:
谢谢老兄!你拯救了我的一天。上帝祝福你。我会尽快接受你的答复。以上是关于android/php 记录未插入 mysql的主要内容,如果未能解决你的问题,请参考以下文章
记录未使用 javascript 正确插入到 ms 访问数据库中