2012-11-05 88 views
0

在我看來,我有以下代碼:笨會話值覆蓋

if (($news['newsvideo'] !="") || ($news['newsvideo'] != NULL)) { 

    $newdata4 = array(
    'tablename' => 'news', 
    'recordid'  => $news['newsid'], 
    'fieldname' => 'newsid', 
    'fieldToUpdate' => 'newsvideo', 
    'dpage' => 'news/editNews' 
    ); 

    $this->session->set_userdata($newdata4); 
    echo anchor('news/updateOneField', 'Delete Video?','class=btn'); 
} 

    if (($news['newsvideo_en'] !="") || ($news['newsvideo_en'] != NULL)) { 

    $newdata5 = array(
    'tablename' => 'news', 
    'recordid'  => $news['newsid'], 
    'fieldname' => 'newsid', 
    'fieldToUpdate' => 'newsvideo_en', 
    'dpage' => 'news/editNews' 
    ); 

    $this->session->set_userdata($newdata5); 
    echo anchor('news/updateOneField', 'Delete Video?','class=btn'); 
} 

功能在我的控制器如下:

public function updateOneField() 
    { 

     $tablename = $this->session->userdata('tablename'); 
     $id = $this->session->userdata('recordid'); 
     $fieldname = $this->session->userdata('fieldname'); 
     $fieldToUpdate = $this->session->userdata('fieldToUpdate'); 
     //$this->newsModel->save(array($fieldToUpdate => ''), $id);   
     //$newsid = $id; 

     echo "Table name: ". $tablename . "<br/>"; 
     echo "Id: " . $id. "<br/>"; 
     echo "Field name: " . $fieldname. "<br/>"; 
     echo "Field to update: " . $fieldToUpdate. "<br/>"; 
     die(); 
     // destroy the session after the update 
     $this->session->unset_userdata('tablename'); 
     $this->session->unset_userdata('recordid'); 
     $this->session->unset_userdata('fieldname'); 
     $this->session->unset_userdata('fieldToUpdate'); 

     $this->displayEditNewsForm($newsid); 
    } 

現在,如果我第一個鏈接我要上點擊刪除newsvideo,當試圖更新newsvideo字段時,newsvideo_en被更新。這意味着我稍後重寫數組的值。有沒有人有想法如何避免,並更新正確的領域?

問候,卓然

回答

1

1)你爲什麼對空和空檢查?

if (($news['newsvideo_en'] !="") || ($news['newsvideo_en'] != NULL)) { 

你可能只使用

if ($news['newsvideo_en'] !=""){

,除非你的意思是檢查是否not emptynot null在這種情況下,你要

if (($news['newsvideo_en'] !="") AND ($news['newsvideo_en'] != NULL)) {

2)爲什麼在取消設置會話數據之前將腳本告訴die()

echo "Table name: ". $tablename . "<br/>"; 
    echo "Id: " . $id. "<br/>"; 
    echo "Field name: " . $fieldname. "<br/>"; 
    echo "Field to update: " . $fieldToUpdate. "<br/>"; 
    die();//anything below here WILL NOT RUN 
    // destroy the session after the update 

    //this code will never run... 
    $this->session->unset_userdata('tablename'); 
    $this->session->unset_userdata('recordid'); 
    $this->session->unset_userdata('fieldname'); 
    $this->session->unset_userdata('fieldToUpdate'); 

因爲腳本曾經到達unset_userdata代碼之前死亡的,未設置從不運行。如果您想清除會話數據,則必須清除die()

3)爲什麼不使用POST?

echo form_open("video_edit"); 

if ($news['newsvideo'] !="") { 
    echo form_hidden('fieldToUpdate','newvideo'); 
} 
if ($news['newsvideo_en'] !="") { 
    echo form_hidden('fieldToUpdate','newvideo_en'); 
} 
echo form_hidden("tablename","news"); 
echo form_hidden("recordid",$news['newsid']); 
echo form_hidden("fieldname","newsid"); 
echo form_hidden("dpage","news/editNews"); 

echo anchor("news/updateOneField", "Delete Video?","class=btn"); 

echo form_close(); 
+0

2)爲什麼你在將會話數據解除設置之前告訴腳本die()?因爲我想檢查我正在更新哪個字段,因爲它總是最後一個,那就是videonews_en – Zoran

+0

3)爲什麼你不使用POST?當我有一個領域需要更新時,這個功能就像一個魅力。添加更多字段時會發生問題。在我只有一個領域的其他控制器中,它工作得很好。如果我找不到我的問題的解決方案,我想我會使用這個帖子... – Zoran

+0

那麼,會話數據的大小是非常有限的(如果你不使用數據庫會話):「注意:Cookie只能容納4KB的數據,因此小心不要超過容量,加密過程尤其會產生比原始數據字符串更長的數據串,因此請仔細跟蹤您存儲的數據量。「 - http://codeigniter.com/user_guide/libraries/sessions.html更好地使用會後數據並不是爲你想要做的。 – stormdrain