2014-03-04 67 views
0

我有一個用於編輯用戶記錄的表單。它第一次更新了記錄,但是當我再次按下更新按鈕時,它會向我顯示一個空白屏幕,並且它也會將圖片取消。我想不出什麼我doign錯 下面是我的代碼Yii更新案例顯示空屏幕

public function actionEditProfile($affusername = NULL) { 

    $this->layout = 'layout_home'; 

    if ($this->VarifyUser($affusername) && Yii::app()->user->id) { 

     $model = new Users('edit'); //apply rules if user comes directly to signup page 
     // uncomment the following code to enable ajax-based validation 
     if (isset($_POST['ajax']) && $_POST['ajax'] === 'editprofile-form') { 
      CActiveForm::validate($model); 
      Yii::app()->end(); 
     } 

     if (isset($_POST['Users'])) { 


      $the_image = CUploadedFile::getInstance($model, 'image'); 
      if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') { 

       $model->image = $the_image; 
      } 


      if (is_object($the_image)) { 

       $file_name = Yii::app()->user->id . '.' . $model->image->extensionName; 

       $move = $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/themes/myproject/images/users/' . $file_name); 

       $_POST['Users']['image'] = $file_name; 
      } 


      $model->attributes = $_POST['Users']; 


      if ($model->validate()) { 

       // form inputs are valid, do something here 
       if ($model->updateByPk(Yii::app()->user->id, $_POST['Users'])) { 
        //$this->setFlashSuccess("Unable to register user, please try again"); 
        $this->setFlashSuccess('Updated successfully, now redirecting.'); 
        //$this->redirect($this->base_url); 
       } else { 
        $this->setFlashError("Unable to update user, please try again"); 
        return false; 
       } 
      } else { 
       print_r($model->getErrors()); 
       echo 'validation fail'; 
       exit; 
      } 
     } 

     $user_data = Users::model()->getUsersByUserName($affusername); //getting user information 
     //print_r($user_data); 
     //$model=new Users('nonfb-create'); //apply rules if user comes directly to signup page 
     $this->render('editprofile', array('model' => $model, 'data' => $user_data,)); 
    } else { 
     $this->redirect($this->base_url); 
    } 
} 

我的看法是

<div class="adminform_wrapp"> 
    <?php 
    $form = $this->beginWidget('CActiveForm', array(
     'id' => 'editprofile-form', 
     'enableAjaxValidation' => false, 
     'clientOptions' => array(
      'validateOnSubmit' => true, 
     ), 
     'enableClientValidation' => true, 
     'focus' => array($model, 'first_name'), 
     'htmlOptions' => array(
      'enctype' => 'multipart/form-data' 
     ) 
    )); 
    echo $form->errorSummary($model); 
    ?> 

    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label class="fieldname required" for="Users_First_Name">First Name: <span class="required">*</span></label> 
     <?php echo $form->textField($model, 'first_name', array('value' => $data['first_name'])); ?> 
     <?php $form->error($model, 'first_name'); ?> 
    </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label class="fieldname required" for="Users_Last_Name">Last Name: <span class="required">*</span></label> 
     <?php echo $form->textField($model, 'last_name', array('value' => $data['last_name'])); ?> 
     <?php $form->error($model, 'last_name'); ?> 
    </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label class="fieldname required" for="Users_Email">Email: <span class="required">*</span></label> 
     <?php echo $form->textField($model, 'email', array('value' => $data['email'])); ?> 
     <?php $form->error($model, 'email'); ?> 
    </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label class="fieldname required" for="Users_Phone_No">Phone No: <span class="required">*</span></label> 
     <?php echo $form->textField($model, 'phone_no', array('value' => $data['phone_no'])); ?> 
     <?php $form->error($model, 'phone_no'); ?> 
    </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label>Username:</label> 
     <span class="profile-username"><?php echo $data['username']; ?></span> </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label>Edit Profile picture:</label> 
     <span class="image-placeholder"> 
      <img src="<?php echo $this->theme_baseurl . '/images/users/' . $data['image']; ?>" style="width:96px; height:96px;"/> 
     </span> 
     <div id='file_browse_wrapper'> 

      <?php 
      //echo $form->labelEx($model, 'image'); 
      echo $form->fileField($model, 'image', array('id' => 'file_browse')); 
      echo $form->error($model, 'image'); 
      ?> 
     </div> 
    </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <?php echo $form->labelEx($model, 'Address', array('class' => 'fieldname')); ?> 
     <?php echo $form->textField($model, 'address', array('value' => $data['address'])); ?> 
     <?php $form->error($model, 'address'); ?> 
    </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label class="fieldname required" for="Users_Country">Country: <span class="required">*</span></label> 
     <select class="error" onchange="print_state('Users_state', this.selectedIndex);" id="Users_country" name ="Users[country]"></select> 
     <?php $form->error($model, 'country'); ?> 
    </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label class="fieldname required" for="Users_State">State: <span class="required">*</span></label> 
     <select name ="Users[state]" id ="Users_state"></select> 
     <script language="javascript">print_state("Users_state", '', "<?php echo $data['state'] ?>");</script> 
     <?php $form->error($model, 'state'); ?> 
    </div> 
    <!--adminform_row--> 
    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label class="fieldname required" for="Users_City">City: <span class="required">*</span></label> 
     <?php echo $form->textField($model, 'city', array('value' => $data['city'])); ?> 
     <?php $form->error($model, 'city'); ?> 
    </div> 
    <!--adminform_row--> 

    <!--adminform_row--> 
    <div class="adminform_row"> 
     <label class="fieldname required" for="Users_Zipcode">Zipcode: <span class="required">*</span></label> 
     <?php echo $form->textField($model, 'zipcode', array('value' => $data['zipcode'])); ?> 
     <?php $form->error($model, 'zipcode'); ?> 
    </div> 
    <!--adminform_row--> 

    <!--adminform_row--> 
    <div class="adminform_row"> 
     <input type="submit" class="adminupdate_btn" value="Update"> 
     <input type="reset" class="admincancel_btn" value="Cancel"> 
    </div> 
    <!--adminform_row--> 

    <?php $this->endWidget(); ?> 
</div> 
+0

您應該在操作中放置一些日誌以查看執行錯誤的位置! – darkheir

回答

0

顯示空屏幕平時的時候有一個語法錯誤。 您可以在apache日誌中看到錯誤(通常在linux的/var/log/apache2/error.log中)。 正如darkheir所說,你應該看到那個日誌文件。

0

這是一個語法錯誤。它還更新數據庫中完整的行,其中更新表單只發送幾個字段。我通過手動設置屬性進行了修復。以下是我的代碼

if (isset($_POST['Users'])) { 
      //echo "<pre>"; 
      //print_r($model->image); 
      // print_r($_POST); 
      // print_r($_FILES); 
      //echo "</pre>"; 

      $the_image = CUploadedFile::getInstance($model, 'image'); 

      if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') { 

       $model->image = $the_image; 
      } 
      //echo "<pre>"; 
      //print_r($_POST); 
      //echo "</pre>"; 
      $first_name = $_POST['Users']['first_name']; 
      $last_name = $_POST['Users']['last_name']; 
      $email = $_POST['Users']['email']; 
      $phone_no = $_POST['Users']['phone_no']; 
      $address = $_POST['Users']['address']; 
      $country = $_POST['Users']['country']; 
      $state = $_POST['Users']['state']; 
      $city = $_POST['Users']['city']; 
      $zipcode = $_POST['Users']['zipcode']; 
      if (is_object($the_image)) { 

       $file_name = Yii::app()->user->id . '.' . $model->image->extensionName; 

       $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/themes/karmora/images/users/' . $file_name); 

       $_POST['Users']['image'] = $file_name; 
       //$model->first_name = $_POST['Users']['first_name']; 
       $attributes = array(
        'first_name' => $first_name, 
        'last_name' => $last_name, 
        'email' => $email, 
        'phone_no' => $phone_no, 
        'image' => $file_name, 
        'address' => $address, 
        'country' => $country, 
        'state' => $state, 
        'city' => $city, 
        'zipcode' => $zipcode 
       ); 
      } else { 
       $attributes = array(
        'first_name' => $first_name, 
        'last_name' => $last_name, 
        'email' => $email, 
        'phone_no' => $phone_no, 
        'address' => $address, 
        'country' => $country, 
        'state' => $state, 
        'city' => $city, 
        'zipcode' => $zipcode); 
      } 


      $model->updateByPk(Yii::app()->user->id, $attributes); 
     }