Save Multiple Image Into Mysql Php From Android But Only One Image Get Inserted
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 the image will be stored in PhotoUpload fold
Solution 1:
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) {
JSONObjectobject = newJSONObject();
Stringtype = i.getType();
String[] Type = type.split(":");
String amount = i.getAmount();
String[] Amount = amount.split(":");
String description = i.getDescription();
String[] Description = description.split(":");
//ImageString 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
@OverrideprotectedStringdoInBackground(String... params) {
try {
HashMap<String, String> data = newHashMap<String, String>();
data.put("listItems", jsonArray.toString());
RequestHandler rh = newRequestHandler();
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( $listItemsas$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
<?phpif( $_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( $listItemsas$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();
}
}
?>
Post a Comment for "Save Multiple Image Into Mysql Php From Android But Only One Image Get Inserted"