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_en 会更新。这意味着我稍后会覆盖数组的值。有谁知道如何避免这种情况并更新正确的字段?

问候,佐兰

4

1 回答 1

1

1)你为什么要检查空和空?

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

你可能只使用

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

除非你打算检查是否not empty AND 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();
于 2012-11-05T16:10:12.140 回答