dongliufa6380 2015-12-29 08:59
浏览 37
已采纳

将ArrayList从android发送到php MySQL

In table work_details, I have 7 column (id, project, work_description, percentage, time_in, time_out, fk). Now I want to save the arraylist and fk to the table.

I have tried to code but I know this is not the correct way.

  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

<?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);
    }
?>

Noted that the listItems holds project,work_description,percentage,time_in and time_out.

  • 写回答

1条回答 默认 最新

  • douqiao5440 2015-12-29 09:06
    关注

    Your SQL query is incorrect:

    $sql="INSERT INTO work_details (project, work_description, percentage, timeIn, timeOut, id) VALUES ('$val', '$id')";
    

    You have 6 columns to set and provide only 2 values.

    Instead of iterating over $list and executing a query for each value, you should rather construct the query while iterating and execute it only once at the end when it is complete. Example:

    foreach (...) {
        $sql = $sql . "'$val', ";
        ...
    }
    $sql = "INSERT INTO work_details (project, work_description, percentage, timeIn, timeOut, id) VALUES (" . $sql . "'$id')";
    

    This is just the idea.. I'm not a PHP guy so there's probaby errors in what I wrote.

    EDIT : This assumes that order is always the same in the list.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私