Save multiple image into mysql php from android but only one image get inserted

前端 未结 1 2063
天命终不由人
天命终不由人 2021-01-28 02:59

I have three list view in Activity A as below

When the submit button is clicked, the text and image path will be saved into MySQL and

1条回答
  •  伪装坚强ぢ
    2021-01-28 03:39

    First of all you are overwriting the image data in you doInBackground loop.

    Second the PHP upload code is not in the loop

    JAVA

    You should have only one loop, when you build your JSON, put everything you need there

    for (ImageAndText i : listItems) {
        JSONObject object = new JSONObject();
    
        String type = i.getType();
        String[] Type = type.split(":");
        String amount = i.getAmount();
        String[] Amount = amount.split(":");
        String description = i.getDescription();
        String[] Description = description.split(":");
    
        //Image
        String image = i.getImage().toString()
        Uri imageUri = Uri.parse(image);
    
        object.put("amount", Amount[1]);
        object.put("type", Type[1]);
        object.put("description", Description[1]);
        object.put("ts_id", id);
        object.put("image", image);
        object.put(Configs.KEY_IMAGE, getStringImage(imageUri));
    
        jsonArray.put(object);
    }
    

    Then put the JSON in your hashmap to send

    @Override
    protected String doInBackground(String... params) {
        try {
            HashMap data = new HashMap();
            data.put("listItems", jsonArray.toString());
    
            RequestHandler rh = new RequestHandler();
            String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
            return result;
        } catch (Exception e) {
            return "";
        }
    }
    

    PHP

    The php would change, you won't need $image = $_POST['image'];

    You will get the image data from the json $listItems = json_decode( $_POST['listItems'], true );

    You would put the upload code in the loop , and insert only on successful upload

    foreach( $listItems as $item ){ 
        $path=time()."$id.png";
        $actualpath="http://192.168.107.115:80/Android/CRUD/PhotoUpload/$path";
        $bytes=file_put_contents( $savepath, base64_decode( $item['image'] ) );
        if( !$bytes ){
            echo 'Error saving image';  
        }else{
            $stmt->bind_param('sssss', 
            $item['type'], 
            $item['amount'], 
            $item['description'], 
            $actualpath, 
            $item['ts_id'] );
            $res=$stmt->execute();
            if( !$res ) echo 'Query failed with code: '.$stmt->errno;
        }
    } 
    

    EDIT:

    Full PHP script

    connect_errno ) echo "Failed to connect to MySQL";
                $sql="INSERT INTO `staff_benefit` 
                     ( `type`, `amount`, `description`, `image`, `ts_id` ) 
                      VALUES ( ?, ?, ?, ?, ? )";
                if($stmt=$mysqli->prepare($sql )){
                    $url="http://192.168.107.115:80/Android/CRUD/PhotoUpload/";
                    foreach( $listItems as $item ){ 
                        $image_name = time().".png";
                        $save_path = 'PhotoUpload/'.$image_name;
                        $image_url = $url.$image_name;
                        $bytes=file_put_contents($save_path, base64_decode($item['image']));
                        if( !$bytes ){
                            echo 'Error saving image';  
                        }else{
                            $stmt->bind_param('sssss', 
                            $item['type'], 
                            $item['amount'], 
                            $item['description'], 
                            $image_url, 
                            $item['ts_id'] );
                            if( !$res=$stmt->execute()){ 
                                echo 'Query failed with code: '.$stmt->errno;
                            }
                        }
                    } 
                }
                $mysqli->close();
            }
        }
    ?>
    

    0 讨论(0)
提交回复
热议问题