I'm using SELECT(MAX())
inside a foreach
loop and this is my code:
foreach($_POST['image_Basename'] as $key=>$image_Basename){
$image_Title = $this->input->post('image_Title');
$image_Category_Id = $this->input->post('image_Category_Id');
$this->db->query("INSERT INTO mg_gallery (image_Group_Id, image_title, image_Basename, image_Category_Id)
SELECT 1 + coalesce((SELECT max(image_Group_Id) FROM mg_gallery), 0), '$image_Title', '$image_Basename', '$image_Category_Id'
");
}
The problem is that for each image_Basename
, query produces a new number.
For example, if I got 3 image_Basenames
, it will insert 1, 2 and 3 for those three image_Basenames
. But I want it to insert the same number to all of image_Basenames
.
For example, if the max number in the image_Group_Id is 1, then add number 2 for each image_Basename. How can I do that?! I've put
SELECT 1 + coalesce((SELECT max(image_Group_Id) FROM mg_gallery
outside of the foreach
loop, but it didn't work!!!
The answer is added below by myself