在表格work_details
中,我有7列(id, project, work_description, percentage, time_in, time_out, fk)
。现在我想将arraylist
和fk
保存到表格中。
我试过编码,但我知道这不是正确的方法。
public void addWorkDetails(ArrayList<SearchResults> listItems, long id)
{
JSONObject object= new JSONObject();
for(int i=0;i<listItems.size();i++)
{
try
{
object.put("Count : "+String.valueOf(i + 1),listItems.get(i));
}catch(JSONException e)
{
e.printStackTrace();
}
}
class AddWorkDetails extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String,String>();
// what should I put here ?
RequestHandler rh=new RequestHandler();
String result = rh.sendPostRequest(Config.ADD_WORKDETAILS,data);
return result;
}
}
AddWorkDetails ru = new AddWorkDetails();
ru.execute(listItems,id);
}
腓
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$list[]=$_POST['listItems'];
$id=$_POST['id'];
foreach($list as $value){
$value=mysqli_real_escape_string($val);
$sql="INSERT INTO work_details (project, work_description, percentage, timeIn, timeOut, id) VALUES ('$val', '$id')";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if(mysqli_query($con,$sql)){
echo ' Added Successfully';
}else{
echo 'Could Not Add Data';
}
//Closing the database
mysqli_close($con);
}
?>
注意 listItems 持有project,work_description,percentage,time_in and time_out
。
答案 0 :(得分:1)
您的SQL查询不正确:
$sql="INSERT INTO work_details (project, work_description, percentage, timeIn, timeOut, id) VALUES ('$val', '$id')";
您需要设置6列,并且只提供2个值。
不是迭代$list
并对每个值执行查询,而是应该在迭代时构造查询,并在完成时仅在结束时执行一次。例如:
foreach (...) {
$sql = $sql . "'$val', ";
...
}
$sql = "INSERT INTO work_details (project, work_description, percentage, timeIn, timeOut, id) VALUES (" . $sql . "'$id')";
这只是一个想法..我不是一个PHP家伙,所以我写的内容存在错误。
编辑:这假定列表中的顺序始终相同。