簡體   English   中英

在CodeIgniter中以單一形式插入和更新

[英]insert and update in single form in CodeIgniter

這里我有控制器news.php:

<?php

class News extends CI_Controller{

    public function __construct(){
         parent::__construct();
                $this->load->model('news_model');
                $this->load->helper('url_helper');
    }

    public function index(){

        $data['news'] = $this->news_model->get_news();

        $data['title'] = 'News archive';

        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }

    public function view($slug = NULL)
        {
               $data['news_item'] = $this->news_model->get_news($slug);

        if (empty($data['news_item']))
        {
                show_404();
        }

        $data['title'] = "News in Detail(s)";

        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
        }

                public function create()
        {
                $this->load->helper('form');
                $this->load->library('form_validation');

                $data['title'] = 'Create a news item 22';

                $this->form_validation->set_rules('title', 'Title', 'required');
                $this->form_validation->set_rules('text', 'Text', 'required');

                $data['updateid'] = '' ;


                if ($this->form_validation->run() === FALSE)
                {
                    $this->load->view('templates/header', $data);
                    $this->load->view('news/create');
                    $this->load->view('templates/footer');

                }
                else
                {
                    $this->news_model->set_news();
                    if($update == 1){
                    redirect('/news', 'location');
                 }
                }
        }

        public function update($id){

                $this->load->helper('form');
                $this->load->library('form_validation');

            $data['updateid'] = $id;
            $data['title'] = "Update News in Detail(s)";
            $data['update'] = $this->news_model->edit_load_data($id);

            $this->load->view('news/create',$data);

        if ($this->input->post('submit')) {

               $update =   $this->news_model->update_news($id);
               if($update == 1){
                    redirect('/news', 'location');
                 }
            }



        }
}

?>

這是我的模型文件news_model.php:

<?php


class News_model extends CI_Model{

    public function __construct(){

        $this->load->database();
    }

    public function get_news($slug= false){

        if($slug == false){
            $query= $this->db->get('news');
             return $query->result_array();
        }

         $query = $this->db->get_where('news', array('slug' => $slug));
            return $query->row_array();
    }

    public function set_news(){

            $this->load->helper('url');

            $slug = url_title($this->input->post('title'), 'dash', TRUE);




            $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            //'text' => $this->input->post('text')
           'text' => $this->input->post('select')
            );

            return $this->db->insert('news', $data);
    }

    public function edit_load_data($id){

         $query = $this->db->get_where('news', array('id' => $id));


            return $query->row_array();

    }

    public function update_news($id){

        $this->load->helper('url');

            $slug = url_title($this->input->post('title'), 'dash', TRUE);


        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            //'text' => $this->input->post('text')
           'text' => $this->input->post('select')
            );

                 $this->db->where('id', $id);


            return $this->db->update('news', $data);




    }

}
?>

這是我的表單create.php文件:

 <?php echo validation_errors(); 
    echo $updateid;
    ?>
    <?php if($updateid == ''){ ?>
    <?php echo form_open('news/create'); ?>
    <?php } else { ?>
    <?php echo form_open('news/update/'.$updateid); ?>
    <?php }  ?>


        <label for="title">Title</label>
        <input type="input" name="title" value="<?php echo $update['title']; ?>" /><br />

        <label for="text">Text</label>
        <textarea name="text" ><?php echo set_value('text'); ?></textarea><br />

          <label for="text">Select</label>
    <select name="select">
    <option <?php if($update['text']=='text1'){ echo "selected";} ?> value="text1">text1</option>
       <option <?php if( $update['text']=='text2'){ echo "selected";} ?> value="text2">text2</option>
    </select>
    <br />



        <input type="submit" name="submit" value="Create news item" />

    </form>

這里當我要去新聞/創建時給出了錯誤:

遇到PHP錯誤

嚴重性:注意

消息:未定義的變量:更新

文件名:news / create.php

行號:15

回溯:

文件:C:\\ xampp \\ htdocs \\ mylab \\ application \\ views \\ news \\ create.php行:15功能:_error_handler

文件:C:\\ xampp \\ htdocs \\ mylab \\ application \\ controllers \\ News.php行:54功能:查看

文件:C:\\ xampp \\ htdocs \\ mylab \\ index.php行:315功能:require_once“/>

我能做些什么? 我想為同一個文件添加和編輯create.php有可能嗎?

我不知道這會產生你想要的結果,但它會阻止使用未定義的變量。

<?php
echo validation_errors();
$updateid = isset($updateid) ? $updateid : '';
echo $updateid;

if($updateid == '')
{
  echo form_open('news/create');
}
else
{
  echo form_open('news/update/'.$updateid);
}

if(!isset($update))
{
  $update['title'] = NULL;
  $update['slug'] = NULL;
  $update['text'] = NULL;
}
?>

<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $update['title']; ?>" /><br />

<label for="text">Text</label>
<textarea name="text" ><?php echo set_value('text'); ?></textarea><br />

<label for="text">Select</label>
<select name="select">
  <option <?php
  if($update['text'] == 'text1')
  {
    echo "selected";
  }
  ?> value="text1">text1</option>
  <option <?php
  if($update['text'] == 'text2')
  {
    echo "selected";
  }
  ?> value="text2">text2</option>
</select>
<br />

<input type="submit" name="submit" value="Create news item" />

</form>

您應該修改模型,以便在輸入不存在時返回默認數組。 我無法確定您向視圖發送$update的位置。

我認為你沒有返回$ update ['title'],$ update ['text']等。你要返回$ title,$ text ...

嘗試將$ update ['title']更改為create.php中的$ title,看看會發生什么

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM