codeigniter会话值被覆盖

时间:2012-11-05 15:32:42

标签: codeigniter

在我看来,我有以下代码:

  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 个答案:

答案 0 :(得分:1)

1)为什么要检查空AND空?

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

你可以使用

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

除非您打算检查not empty not 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代码之前就已经死了,所以unset永远不会运行。如果要清除会话数据,则必须删除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();