使用codeigniter更新数据库中的记录

时间:2016-05-20 10:25:58

标签: php html mysql codeigniter

我正在尝试使用codeigniter更新数据库中的表,但我的数据没有更新。

这是我的观看代码:

<div class="review_all">
  <?php foreach($result as $row) { ?>
  <div class='fb_review_panel'>
    <div class='review_date'>
    <form action='<?php echo base_url('testimonials/update'); ?>' method='POST' enctype='multipart/form-data'>
      <span class='month_t'>
      <select class='select_update' name='month_update'>
        <option value='<?php echo $row->month?>'> <?php echo $row->month?> </option>
        <option value='January'> January </option>
        <option value='February'> February </option>
        <option value='March'> March </option>
        <option value='April'> April </option>
        <option value='May'> May </option>
        <option value='June'> June </option>
        <option value='July'> July </option>
        <option value='August'> August </option>
        <option value='September'> September </option>
        <option value='October'> October </option>
        <option value='November'> November </option>
        <option value='December'> December </option>
      </select>
      </span> <span>
      <?php
                    echo "<select name=day>";
                    ?>
      <option value='<?php echo $row->day?>'> <?php echo $row->day ?> </option>
      <?php
                    for($i=1;$i<=31;$i++){
                    echo "<option value=$i>$i</option>";
                    }
                    echo "</select>";
                    ?>
      </span> <span class='index_t'>
      <select class='select_update' name='power_update'>
        <option value='<?php echo $row->power_t ?>'> <?php echo $row->power_t ?> </option>
        <option value='st'> st </option>
        <option value='nd'> nd </option>
        <option value='rd'> rd </option>
        <option value='th'> th </option>
      </select>
      </span> <span class='year_t'>
      <select class='select_update' name='year_update'>
        <option value='<?php echo $row->year ?>'> <?php echo $row->year ?> </option>
        <option value='2013'> 2013 </option>
        <option value='2014'> 2014 </option>
        <option value='2015'> 2015 </option>
        <option value='2016'> 2016 </option>
      </select>
      </span>
      </div>
      <div style='display:none<?php echo $row->trash_default ?>; font-family:lato;font-size: 14px;background:#1abc9c;color: white;border-radius: 12px;letter-spacing: 1px; '> Visible </div>
      <div style='display:<?php echo $row->trash_default ?>; font-family:lato;font-size: 14px;background:#e74c3c;color: white;border-radius: 12px;letter-spacing: 1px; '> Hidden </div>
      <div class='review_line'> <span> <i>
        <textarea name='review_update' class='ta'>
            <?php echo $row->review ?>
            </textarea>
        </i> </span> </div>
      <div class='user_details'> <span> <b> -
        <input class='input_update' name='source_update' type='text' value='<?php echo $row->source ?>' />
        </b> </span> <span>
        <input style='display:none;' name='key_update' type='text' value='<?php $row->r_id ?>' />
        </span> <span>
        <select class='select_update' name='trash_reupdate'>
          <option value='none' selected='selected'> Hide / Unhide </option>
          <option value=''> Discard </option>
          <option value='none'> Restore </option>
        </select>
        </span>
        <input type='submit' name='submit' class='save' />
      </div>
    </form>
  </div>
  <?php } ?>
</div>

这是我的型号代码:

public function edit_testimonial($data){
        extract($data);
        $this->db->where('r_id', $key_update);
        $this->db->update($testimonial, array('month' => $month_update,'day' => $day_update,'power_t' => $pow_update,
            'year' => $year_update,'review' => $review_update,'source' => $source_update,'trash_default' => $tr_update));
        return true;
    }

这是我的控制器代码:

public function update(){
        $data=array(
            'testimonial'=>'testimonial',
            'month_update'  => $this->input->post('month_update'),
            'day_update'    => $this->input->post('day_update'),
            'pow_update'    => $this->input->post('power_update'),
            'year_update'   => $this->input->post('year_update'),
            'review_update' => $this->input->post('review_update'), 
            'source_update' => $this->input->post('source_update'),
            'tr_update'     => $this->input->post('trash_reupdate'),
            'key_update'    => $this->input->post('key_update'),
        );

        if($this->testimonial_model->edit_testimonial($data)) // call the method from the model
            {
                echo "update successful";
            }
            else
            {
                echo "update not successful";
            }
    }

在我点击提交按钮后进行编辑后,它会显示消息已成功更新,但是当我检查数据库表时,则没有更新。

3 个答案:

答案 0 :(得分:1)

我不确定这些评论是什么,但请尝试以下

而不是

<input style='display:none;' name='key_update' type='text' value='<?php $row->r_id ?>' />

试试这个

<input name='key_update' type='hidden' value='<?php echo $row->r_id ?>' />

你的回声缺失了

答案 1 :(得分:1)

改变你的

<input style='display:none;' name='key_update' type='text' value='<?php $row->r_id ?>' />

到这个

<input style='display:none;' name='key_update' type='text' value='<?php echo $row->r_id ?>' />

并在控制器中粘贴此代码:

public function update(){
        $data=array(

            'month_update'  => $this->input->post('month_update'),
            'day_update'    => $this->input->post('day_update'),
            'pow_update'    => $this->input->post('power_update'),
            'year_update'   => $this->input->post('year_update'),
            'review_update' => $this->input->post('review_update'), 
            'source_update' => $this->input->post('source_update'),
            'tr_update'     => $this->input->post('trash_reupdate'),
            'key_update'    => $this->input->post('key_update')
        );


        if($this->testimonial_model->edit_testimonial($data)) // call the method from the model
            {

             echo "update successful";
            }
            else
            {
                echo "update not successful";
            }
    }

并在您的模型中使用此代码:

public function edit_testimonial($data){
        extract($data);
        $this->db->where('r_id', $key_update);
        $this->db->update('testimonial', array('month' => $month_update,'day' => $day_update,'power_t' => $pow_update,
            'year' => $year_update,'review' => $review_update,'source' => $source_update,'trash_default' => $tr_update));
        if($this->db->affected_rows())
            {
                return true;
            }
            else
            {
                return false;
            }
    }

答案 2 :(得分:0)

@nushrat 1-首先,你有循环形式,所以会有多种形式,但你只有最后的表格信息或可能是随机的。 2-在控制器的更新功能中,通过在汇编$ data数组之前编写echo '<pre>';print_r($_POST);echo '</pre>';来验证您是否正确接收数据,并在汇编数据数组后编写此代码以检查$ data变量是否填充正确或不。 echo '<pre>';print_r($data);echo '</pre>';和死();或者在它之后退出以继续前进。 3-如果在第二步中您发现数据设置正确而不是跳转到模态edit_testimonial函数并且在提取之前($ data);写echo '<pre>';print_r($data);echo '</pre>';以验证您是否已正确接收数据。 4-为了便于阅读,请正确缩进代码。

请告诉我你的答复。