尝试将ArrayList从android发送到php MySQL时出错

时间:2015-12-29 15:03:13

标签: java php android mysql arraylist

我是php的新手,现在正尝试将arrayList数据从Android发送到Php MySQL

在表格work_details中,我有7列(id, project, work_description, percentage, time_in, time_out, fk)。现在我想将arraylistfk保存到表格中。

我试过编码,但我知道这不是正确的方法。

public void addWorkDetails(ArrayList<SearchResults> listItems, final long id) // listItems have project,workDescription,percentage,timeIn,timeOut
    {
        final 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>();
                data.put("listItems",String.valueOf(object)); // not sure 
                data.put(Config.KEY_TWF,String.valueOf(id));
                RequestHandler rh=new RequestHandler();
                String result = rh.sendPostRequest(Config.ADD_WORKDETAILS,data);
                return  result;
            }
        }

        AddWorkDetails ru = new AddWorkDetails();
        ru.execute("listItems",String.valueOf(id)); // not sure
    }

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

错误

enter image description here

任何帮助都将非常感谢!!!

1 个答案:

答案 0 :(得分:1)

<强>的AsyncTask:

class AddWorkDetails extends AsyncTask<String, Void, String> {
    ProgressDialog loading;


    JSONArray jsonArray;
    AddWorkDetails(JSONArray jsonArray){
        this.jsonArray = jsonArray
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);
    }

    @Override
    protected String doInBackground(String... params) {
        HashMap<String, String> data = new HashMap<String,String>();
        data.put("listItems",jsonArray.toString());
        RequestHandler rh=new RequestHandler();
        String result = rh.sendPostRequest(Config.ADD_WORKDETAILS,data);
        return  result;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        loading.dismiss();
        Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
    }
}

<强>功能

public void addWorkDetails(ArrayList<SearchResults> listItems, final long id)
{
    JSONArray jsonArray = new JSONArray();
    try
    {
        for (SearchResults s : listItems)
        {
            JSONObject object= new JSONObject();
            object.put("project", s.getProject());
            object.put("work_description", s.getDescription());
            object.put("percentage", s.getProgress());
            object.put("timeIn", s.getTimeIn());
            object.put("timeOut", s.getTimeOut());
            object.put("twf", String.valueOf(id));
            jsonArray.put(object);
        }
    }catch(JSONException e)
    {
        e.printStackTrace();
    }

    AddWorkDetails ru = new AddWorkDetails(jsonArray);
    ru.execute(); 
}

PHP脚本

<?php
 if($_SERVER['REQUEST_METHOD']=='POST'){
    $mysqli = new mysqli("localhost", "user", "password", "database");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    $listItems = json_decode($_POST['listItems'], true); 
    $sql="INSERT INTO work_details 
    (project, work_description, percentage, timeIn, timeOut, twf) 
    VALUES 
    (?, ?, ?, ?, ?, ?)"; 

    if (!($stmt = $mysqli->prepare($sql))) {
         echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
         foreach($listItems as $item){ 
            $stmt->bind_param("ssssss", $item['project'], $item['work_description'], $item['percentage'], $item['timeIn'], $item['timeOut'], $item['twf']);
            if (!$stmt->execute()) {
                echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
            }
        } 
    }
    $mysqli->close();
}
?>