0

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

2
  • its not very clear to understand, how do you get the images ? do you get an array of images ? did you try using a loop with count for insert query? Commented Feb 24, 2016 at 12:47
  • Why you simply don't check is it corresponding $image empty or not and then execute the query? PHP code will be useful as well. Commented Feb 24, 2016 at 12:49

2 Answers 2

1

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! ;)
?>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Hi, but I am not sure how many images will have for products. It varies on products, some have 3, some have 5 like that. so in array what should I defined.
Just keep appending them to the array. Where are the image names coming from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.