Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7157829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T12:57:28+00:00 2026-05-28T12:57:28+00:00

Hi there I have written a code that ietrates through whole array and copies

  • 0

Hi there I have written a code that ietrates through whole array and copies the images to a folder and inserts data in record. Now funny thing is that $model->save()does not show error it returns true. and program never goes into else

now what happens loop continues to run and completes its ietration without breaking. I can not guess who is wrong. greater chances are me as i am most of time 🙂

here is code

   protected function saveImage($formData,$model)
        {                   
               if ($formData === null)
                   return;                   
                $idx=0;

            foreach($formData['title'] as $image)
            {
                $model->title = $image;                    
                $file= dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR 
                        . DIRECTORY_SEPARATOR .'images\hotelImages'. DIRECTORY_SEPARATOR 
                        . $model->hotel->name;

                $model->image = Yii::app()->baseUrl. "/images/hotelImages/".$_FILES['HotelImages']['name'][$idx];//image path

                if($model->save())
                { 
                    echo $idx.'<br /> it was sucess<br />';

                    If(file_exists($file)) 
                    {
                        copy($_FILES['HotelImages']['tmp_name'][$idx],$file."/".$_FILES['HotelImages']['name'][$idx]);
                    }
                    else
                    {
                         mkdir($file); 
                        copy($_FILES['HotelImages']['tmp_name'][$idx],$file."/".$_FILES['HotelImages']['name'][$idx]);
                    }
                    $idx++;

                }//if there was error
                 else
                 {
                     print_r($model->getErrors());
                     yii::app()->end();                
                    return FALSE;
                 }
                 echo '<br />end reached <br />';


            }
             yii::app()->end(); 
             return true;
        }

var_dump for $formdata is

array
(
    'title' => array
    (
        '0' => 'title1'
        '1' => 'title2'
    )
)

No mater what ever the ietration count for foreach loop database gets only single row

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T12:57:29+00:00Added an answer on May 28, 2026 at 12:57 pm

    The save() method inserts a record into the database if it doesn’t exist yet, and otherwise updates that same database record.

    $model is being passed in as a method parameter, and I’m just assuming here that its title attribute is not the primary key.

    In other words, you keep updating the same database record over and over, which is why you only see one record in the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some PHP code that will return a value if there are
There is a div: <div id=question>ddddd</div> I have written a code that hide the
i have written the following code: As you can see there is a for
so I have written several stored procedures that act on individual rows of data
I have written code in C# that should transform an XML with the help
I have written some C++ code that generates a std::vector. I also have a
I have written code for an animation that shakes a UIImageView on the screen,
In jQuery, I have written a piece of code that appears on every page
I have written code that automatically creates CSS sprites based on the IMG tags
I have written some code that makes use of an open source library to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.