使用Post Submit(Codeigniter)更新循环中的Textbox值

时间:2014-01-08 06:17:43

标签: php mysql codeigniter

我有2个文本框循环 当我按下按钮时,我想在数据库中更新它们的值。

问题是,当我按下按钮时,它只更新最后一个文本框值。

以下是我的模特。

function approvedHrs($taskid,$data)
{
    //$this->db->where('id', $id);
    $this->db->where('taskid', $taskid);
     $this->db->update(MILESTONE, $data);
}

以下是我的观点(对于lop在这里)

<?php
foreach ($result as $milestone_row) {   
?>
<tr id="<?php echo $milestone_row->id; ?>"> 
    <?php
    if ($is_master_admin) {
        if ($i > 1) {
            if ($milestone_row->userid == $userid) {
            } else {
                $userid = $milestone_row->userid;
                echo $milestone_row->usertitle;
            }
        } else {
            $userid = $milestone_row->userid;
            echo $milestone_row->usertitle;
        }
    }
    ?>
<li class="in">
    <a href="">Add Bug</a>
    <div class="message">
        <span class="arrow"></span>
        <span class="body">
            <?php
            echo '<b> <U> Task Title </U>:- </b> &nbsp';
            echo $milestone_row->tasktitle;
            echo '<br/>';
            echo '<b> <U> Workspace Title </U>:- </b> &nbsp';
            echo $milestone_row->workspacetitle;
            echo '<br/>';
            echo '<b> <U> Description </U>:- </b> &nbsp';
            echo $milestone_row->description;
            echo '<br/>';
            echo '<b> <U> Hours </U>:- </b> &nbsp';
            echo $milestone_row->esthours;
            echo 'hrs';
            echo '<br/>';
            echo '<b> <U> Minutes </U>:- </b> &nbsp';
            echo $milestone_row->estmin;
            echo'mins';
            echo '<br/>';
            ?>
            <b><u>Approved Hours:-</u></b>&nbsp;&nbsp;&nbsp; 

<input style="height:14px;font-size:10pt; width: 33px" type="text" id="hours" name="approvedhrs" data-required="1"  value="<?php echo $milestone_row->esthours; ?>" placeholder="Hours" onkeypress="return isNumberKey(event)" />

<input style="height:14px;font-size:10pt; width: 33px" type="text" id="minutes" name="approvedmins" value="<?php echo $milestone_row->estmin; ?>" data-required="1" placeholder="Minutes" onkeypress="return isNumberKey(event)"  />

            <a href="<?php echo base_url(); ?>milestone/editMilestone/<?php echo $milestone_row->id;?>/<?php echo $milestone_row->taskid;?>">Edit</a>
            <?php echo " | ";?>
            <a href="<?php echo base_url(); ?>milestone/deleteMilestone/<?php echo $milestone_row->id;?>/<?php echo $milestone_row->taskid;?>">Delete</a>
            <!--<span class="datetime">at <?php //echo $milestone_row->createddate; ?></span> -->
        </span>
    </div>
</li>

Followinf是myController

function approvedHrs($taskid)
    {
        if ($this->session->userdata('logged_in')){       
            $session_data = $this->session->userdata('logged_in');
            $id = $session_data['id'];
            $username = $session_data['username'];
            $is_master_admin = $session_data['master'];
            $imagethumb = $session_data['imagethumb'];
            $pendingbug = $this->bugmodel->getBug($id, $is_master_admin);
            $pendingtask = $this->taskmodel->getTask($id, $is_master_admin);

            $data = '';
                $data = array(
                    'approvedhrs' =>$this->input->post('approvedhrs'),
                    'approvedmins'=>$this->input->post('approvedmins')
                );
//                print_r($data);
//                exit;
                $result = $this->milestonemodel->approvedHrs($taskid,$data);
//              $this->session->set_userdata('msg', $result);
                redirect('task', 'refresh');
      }
      else 
      {
              redirect('login', 'refresh');
      }
    }

当我使用这样的数组时,它会给我以下错误

PHP错误发生:消息:数组到字符串转换

数据库错误:“字段列表”中的未知列“数组” 更新milestone SET approvedhrs =数组,approvedmins =数组WHERE taskid ='17'

3 个答案:

答案 0 :(得分:0)

您应该像这样简化代码

$post   =   $this->input->post();               

$data['approvedhrs']    =   $post['approvedhrs'];
$data['approvedmins']   =   $post['approvedmins'];

print_r($data);

$result = $this->milestonemodel->approvedHrs($taskid,$data);

您应该确保$post['approvedhrs']$post['approvedmins']没有数组。在像

这样的形式中定义时,它们可以包含数组
<input type='text' name="approvedhrs[]" />
<input type='text' name="approvedmins[]" />

编辑

当您在循环内部进行输入时,approvedhrsapprovedmins都将作为数组发布。这导致更新问题。

修改

你应该像这样在你的循环中输入

<input type='text' name="approvedhrs[]" />
<input type='text' name="approvedmins[]" />

为行ID和任务ID

创建隐藏的输入
<input type='hidden' name="rowid[]" />
<input type='hidden' name="taskid[]" />
<input type='text' name="approvedhrs[]" />
<input type='text' name="approvedmins[]" />

现在是php

$post  =   $this->input->post();              

$data['approvedhrs']   =   $post['approvedhrs'];
$data['approvedmins']  =   $post['approvedmins'];

for($i=0;$i<count($data['approvedhrs']);$i++){

    $update_data['approvedhrs']     =   $data['approvedhrs'][$i];
    $update_data['approvedmins']    =   $data['approvedmins'][$i];
    $taskid                         =   $data['taskid'][$i];
    print_r($update_data);
    $result = $this->milestonemodel->approvedHrs($taskid,$update_data);
    unset($update_data);
    unset($taskid);
}

答案 1 :(得分:0)

在传递要更新的数据数组时,可以使用update_batch mentod。

在您的控制器中,准备如下数据。

$k= 0 ;
$data = array();
$aphrs = $this->input->post('approvedhrs'); // returns array
$apmis = $this->input->post('approvedmins'); // 
foreach($aphrs  as $hrs)
{
   $data[] = array(
                    'approvedhrs' =>$hrs,
                    'approvedmins'=>$apmis[$k]
                );
   $k++;
}

//调用你的模型函数

$result = $this->milestonemodel->approvedHrs($taskid,$data);

在模型函数中:添加update_batch方法。所以不需要在forloop中调用update方法。

对于update_batch,请查看本教程。

http://ellislab.com/codeigniter/user-guide/database/active_record.html#update

答案 2 :(得分:0)

查看

<input type='hidden' name="milestoneid[]" value ="<?php echo $milestone_row->id ?>" />
<input  type="text" id="hours" name="approvedhrs[<?php echo $milestone_row->id ?>][]"  value="<?php echo $milestone_row->esthours; ?>"/>
<input type="text" name="approvedmins[<?php echo $milestone_row->id ?>][]" value="<?php   echo $milestone_row->estmin; ?>"/>

控制器

function approvedHrs($taskid) {
        if ($this->session->userdata('logged_in')) {
                $session_data = $this->session->userdata('logged_in');
                $id = $session_data['id'];
                $username = $session_data['username'];
                $is_master_admin = $session_data['master'];
                $imagethumb = $session_data['imagethumb'];
                $pendingbug = $this->bugmodel->getBug($id, $is_master_admin);
                $pendingtask = $this->taskmodel->getTask($id, $is_master_admin);
                $data = array();

                $milestoneIDs = $this->input->post('milestoneid');
                $approvedhrs = $this->input->post('approvedhrs');
                $approvedmins = $this->input->post('approvedmins');

                foreach ($milestoneIDs as $milestoneID) {
                        $data = array(
                                    'approvedhrs' => isset($approvedhrs[$milestoneID]) ? $approvedhrs[$milestoneID] : '',
                                    'approvedmins' => isset($approvedmins[$milestoneID]) ? $approvedmins[$milestoneID] : '',
                        );
                        $this->milestone_model->approvedHrs($milestoneID,$data);//change this model name as per you use
                        unset($data);
                }

                redirect('task', 'refresh');
        } else {
                redirect('login', 'refresh');
        }
}

模型

function approvedHrs($milestoteid, $data) {
//$this->db->where('id', $editid);
        $this->db->where('milestoteid', $milestoteid);
        $this->db->update(MILESTONE, $data);
}