Mysql
 sql >> Database >  >> RDS >> Mysql

Salva più immagini in mysql php da Android ma viene inserita solo un'immagine

Prima di tutto stai sovrascrivendo i dati dell'immagine nel tuo doInBackground ciclo.

In secondo luogo, il codice di caricamento PHP non è nel ciclo

JAVA

Dovresti avere solo un ciclo, quando crei il tuo JSON, metti lì tutto ciò di cui hai bisogno

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

Quindi inserisci il JSON nella tua hashmap inviare

@Override
protected String doInBackground(String... params) {
    try {
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("listItems", jsonArray.toString());

        RequestHandler rh = new RequestHandler();
        String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
        return result;
    } catch (Exception e) {
        return "";
    }
}

PHP

Il php cambierebbe, non avrai bisogno di $image = $_POST['image'];

Otterrai i dati dell'immagine da json $listItems = json_decode( $_POST['listItems'], true );

Dovresti inserire il codice di caricamento nel ciclo e inserirlo solo in caso di caricamento riuscito

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

MODIFICA:

Script PHP completo

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if( !empty( $_POST['listItems'] ) ){
            $listItems = json_decode( $_POST['listItems'], true ); 
            $mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
            if( $mysqli->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();
        }
    }
?>