Jump to content
Larry Ullman's Book Forums
Sign in to follow this  

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!.



       // 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);

                // 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

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...