Pertama-tama Anda menimpa data gambar di doInBackground
Anda lingkaran.
Kedua, kode unggah PHP tidak dalam loop
JAVA
Anda seharusnya hanya memiliki satu loop, ketika Anda membangun JSON Anda, letakkan semua yang Anda butuhkan di sana
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);
}
Kemudian masukkan JSON
di hashmap
untuk mengirim
@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
Php akan berubah, Anda tidak perlu $image = $_POST['image'];
Anda akan mendapatkan data gambar dari json $listItems = json_decode( $_POST['listItems'], true );
Anda akan meletakkan kode unggahan di loop , dan menyisipkan hanya pada unggahan yang berhasil
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:
Skrip PHP lengkap
<?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();
}
}
?>