Jump to content
Larry Ullman's Book Forums

How To Get Multiple Inserted Id Into A Single Array After Query


Recommended Posts

Hello, I tried to insert multiple image into image table using a foreach loop. The images insertion have returned no problem. But I wanted to catch the image_id of each insert and putted them into an array ($iid). Here is problem, no matter what I tried, the result always return 2 separated array:  Array([0]=>9193) Array([1]=>9194). Is there anyway to solve this problem? I want Array([0]=>9193 [1]=>9194). Thank you all!.

 

CODE:

       // query for media
       $query = 'INSERT INTO media (file_title, file_mimetype, file_type, file_url, file_url_thumb, 
           file_is_product_image, created_on) VALUES (?,?,?,?,?,?,NOW())';
       
       $stmt = mysqli_prepare($dbc, $query);
       $i = 0;

       foreach ($image_name as $key => $value){
           $dot = strpos($value, '.');
           $file_title = substr($value, 0, $dot);
           $extension = str_replace('.','',substr($value, $dot));
           $file_mime = 'image/'.$extension;
           $file_type = 'product';
           $file_url = $value;
           $file_url_thumb = 'images/product/resized/'.$file_title. $upload->getSuffix().$extension;
           $fipi = 1;
           
           mysqli_stmt_bind_param($stmt, 'sssssi', $file_title, $file_mime, $file_type, $file_url, $file_url_thumb, $fipi);
           mysqli_stmt_execute($stmt);

                // check result:
               if(mysqli_stmt_affected_rows($stmt) == 1){
               // get the image id
               $iid = array();
               
               $iid[$i++] = mysqli_stmt_insert_id($stmt);
               print_r($iid); // RESULT: Array([0]=>9193)Array([0]=>9194)
               
           } else {
               echo 'Insertion failed' . mysqli_stmt_error($stmt);
           }

       } // End of foreach
Link to comment
Share on other sites

 Share

×
×
  • Create New...