I have an array that contains multiple images directory that I defined as $multipleDir
.
Also I have table named product
that contains the following structure:
CREATE TABLE `product` ( `id` int(255) NOT NULL, `name` varchar(100) NOT NULL, `description` varchar(10000) NOT NULL, `mainimage` varchar(100) NOT NULL, `image1` varchar(100) NOT NULL, `image2` varchar(100) NOT NULL, `image3` varchar(100) NOT NULL, `image4` varchar(100) NOT NULL, `image5` varchar(100) NOT NULL, `image6` varchar(100) NOT NULL, `price` int(100) NOT NULL, `date` date NOT NULL, `tags` varchar(1000) NOT NULL )
$multipleDir
contains 6 different images directory. My goal is to insert to each image column his own directory from the array.
Meaning:
Image1 column -> upload/image1.jpg Image2 column -> upload/image2.jpg Image3 column -> upload/image3.jpg and so on..
My Insert code:
"INSERT INTO product (name, description, mainimage, image1, image2, image3, image4, image5, image6, price, date, tags) VALUES ('". $productName ."', '". $productDescription ."', '". $newfilename ."', 'test', 'test', 'test', 'test', 'test', 'test', '". $price ."', '". $date ."', '". $tag ."')";
Each “test” represent image directory I want to apply.
I tried following the logic of foreach
but it seems like the wrong approach because I can’t control which value will be contained on each column.
How can I insert array values to different MySQL column?
My foreach approach:
foreach($multipleDir as $dir){ $sql = "INSERT INTO product (name, description, mainimage, image1, image2, image3, image4, image5, image6, price, date, tags) VALUES ('". $productName ."', '". $productDescription ."', '". $newfilename ."', '".$dir."', '".$dir."', '".$dir."', '".$dir."', '".$dir."', '".$dir."', '". $price ."', '". $date ."', '". $tag ."')"; }
Advertisement
Answer
Remove foreach and just use $multipleDir array by $multipleDir[0]…$multipleDir[5]
$sql = "INSERT INTO product (name, description, mainimage, image1, image2, image3, image4, image5, image6, price, date, tags) VALUES ('". $productName ."', '". $productDescription ."', '". $newfilename ."', '".$multipleDir[0]."', '".$multipleDir[1]."', '".$multipleDir[2]."', '".$multipleDir[3]."', '".$multipleDir[4]."', '".$multipleDir[5]."', '". $price ."', '". $date ."', '". $tag ."')";