表格不提交数据到codeigniter中的模型

时间:2015-01-26 02:08:05

标签: php codeigniter

我想让用户更新食谱信息。当我点击提交时,它返回到食谱页面,但信息没有改变。基本上我不知道为什么数据库没有更新,我没有错误,以帮助我搞清楚。 任何帮助将不胜感激,谢谢。 这是我的控制器:

public function update(){
            $data = array(
                'id'  => $this->input->post('recipe_id'),
                'title' => $this->input->post('recipe_name'),
                'description' => $this->input->post('recipe_description'),
                'stars' => $this->input->post('rating'),
                'directions' => $this->input->post('recipe_directions'),
                'link' => $this->input->post('recipe_link'),
                'genre' => $this->input->post('recipe_genre'),
                'posted' =>  date('Y-m-d')
            );
            $this->recipe_model->update_recipe($data);
            $this->recipes();
        }

这是我的模特:

function update_recipe($data){
      $id = $data['id'];
      unset($data['id']);
      $this->db->where('id', $id);
      $this->db->update('recipes' ,$data);
      return true;
    }

这是我的观点

<?php 
    $attributes = array('id' => 'update-recipe-form');
    echo form_open('addRecipe/update', $attributes); ?>
    <input type="text" name="recipe_id" class="hide" value="<?php echo $recipe_id; ?>" placeholder="<?php echo $recipe_id; ?>">
    <fieldset class="name">
        <legend>Name of recipe</legend>
        <input type="text" name="recipe_name" class="recipe_name" placeholder="<?php echo $recipe_title; ?>" value="<?php echo $recipe_title; ?>" tabindex="1">
    </fieldset>

1 个答案:

答案 0 :(得分:1)

您不会将数据直接传递给模型,而是将数据传递给使用模型执行某些操作的控制器。

class addRecipe extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model( 'my_model' );
        $this->load->helper( 'form' );
    }

    function index() // The default view
    {
        $data_to_pass = array(
            /* All the variables to pass to the view.
             * This is what the form "preloads" when loaded the first time
             */
            'recipe_title' => 'somevalue',
            'recipe_description' => 'somevalue',
            // etc...
        );
        $this->load->view( 'view_name', $data_to_pass );
    }

    function update()
    {
        // Do something with the data, use model methods, print it, etc.
        // $this->my_model->do_something();
        $id = $this->input->post('recipe_id');

        $data = array(
            'title'       => $this->input->post('recipe_name'), // Title or Name here?
            'description' => $this->input->post('recipe_description'),
            'stars'       => $this->input->post('rating'),
            'directions'  => $this->input->post('recipe_directions'),
            'link'        => $this->input->post('recipe_link'),
            'genre'       => $this->input->post('recipe_genre'),
            'posted'      => date('Y-m-d')
        );

        $success = $this->recipe_model->update_recipe( $id, $data );

        if ( $success )
            $this->load->view( /* your success view */ );
        else
        {
            // Something went wrong
            echo 'MySQL error message: ' . $this->db->_error_message();
            exit;
        }
    }
}

你的模特:

class Recipe_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database(); // If it's not auto-loaded
    }

    function update_recipe( $id, $data )
    {
        $this->db->where( 'id', $id );
        $this->db->update( 'recipes', $data );

        return ( $this->db->affected_rows() > 0 ); // False if no recipes were modified
    }
}

你的表格:

<?=form_open('addRecipe/update', array( 'id' => 'update-recipe-form' ) ); ?>
    <?php
        // Will produce <input type="hidden" name="hidden_variable" value="hidden_value" />
        echo form_hidden('hidden_variable', 'hidden_value');
    ?>

    <fieldset class="name">
        <legend>Name of recipe</legend>
        <input type="text" name="recipe_name" placeholder="<?=$recipe_title; ?>" value="<?=$recipe_title; ?>" tabindex="1">
    </fieldset>

    <!-- Add all other required fields... -->

    <?php
        // Will produce <input type="submit" name="submit_button_name" value="Update recipe" />
        echo form_submit( 'submit_button_name', 'Update recipe' );
    ?>
<?=form_close(); ?>

您可以在此处获取有关表单助手的更多信息:http://www.codeigniter.com/user_guide/helpers/form_helper.html