Reputation: 726
Suppose I have variable $image0
, $image1
, $image2
so on
Which can varies dynamically and generates from array.
And I have sql query to insert data like
Insert into table_name (id,entityid,value,image) values (1,123,"abc",$image0);
Insert into table_name (id,entityid,value,image) values (1,123,"abc",$image1);
Insert into table_name (id,entityid,value,image) values (1,123,"abc",$image2);
As there are 6 insertion query for different images of product.
But its not compulsory that each product images will be same in quantity, some products have 3 images or some have 5 images available.
So How can we check and pass that value to mysql
query and neglect other query if only some variable are available.
Thanks
Upvotes: 0
Views: 295
Reputation: 21523
Simplist way in php is using the implode function, putting the rest of the insert row into the separator:-
$images = array($image0,$image1,$image2);
$sql = "INSERT INTO table_name (id,entityid,value,image) values (1,123,'abc','".implode("'), (1,123,'abc','", $images)."');"
Note that you should make sure your image names are sanitised first.
Upvotes: 0
Reputation: 1583
I think best way to get all images to array and then just use foreach.
Somethink like this?
<?php
// When you obtaining a images, just let them create a array... this is only example
$arr = array($img1, $img2, $img3, $img4);
foreach ($arr as &$img) {
//execute your statement with param $img
}
unset($value); // Clean it! ;)
?>
Upvotes: 1