Codeigniter无法从表单到数据库获取值

时间:2013-07-18 22:41:04

标签: codeigniter

我是codeigniter的新手,我正在尝试从表单到数据库获取食物值。这是m控制器的代码。         

class Tracking extends CI_Controller {

    public $Data = array(); 

    public function __construct()
    {
        parent::__construct();

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

        $this->load->language('tracking', $this->config->item('language'));

        $this->load->model('tracking_model');

         //$data['tracking'] = $results['rows'];

    }

    /*******************************************************
--- GET THE DEFAULT LIST VIEW --------------------------
*******************************************************/    

    function index()
    {
        $this->Data['TrackingArray'] = (array)$this->tracking_model->getTracks();
        $this->load->view('tracking_view', $this->Data);
    }

    /*******************************************************
--- ADD NEW --------------------------------------------
*******************************************************/
    function add()
    {
        if($_SERVER['REQUEST_METHOD']=='POST'){

            $this->_validation();

            if ($this->form_validation->run() == FALSE)
            {               
                $this->Data = array_merge((array) $this->input->post(), (array) $this->Data);

                $this->Data['SysError'] = $this->lang->line('customers_validate_fail');
                $this->load->view('Form', $this->Data);

            }else{

                $Results = $this->tracking_model->addTracks();

                $this->session->set_flashdata('SysOk', $this->lang->line('tracking_add_success'));
                redirect('tracking', 'refresh');
                die();
            }

        }else{
            $this->load->view('Form', $this->Data);
        }
    }

这是我的模型的代码

   function addTracks()
{

    $Data['Food'] = $this->input->post('Food');
    $Data['Calories'] = $this->input->post('Calories');
    $Data['TotalFat'] = $this->input->post('TotalFat');
    $Data['DietaryFiber'] = $this->input->post('DietaryFiber');
    $Data['ServingSize'] = $this->input->post('ServingSize');
    $Data['NumberOfServings'] = $this->input->post('NumberOfServings');
    $Data['TotalPoints'] = $this->input->post('TotalPoints');

    if($this->db->insert('tracking', $Data))
    {  
        return $this->db->insert_id();

    }else{

        return FALSE;
    }
}

最后这是我的表单代码     <         

输入食物信息

    <form name="myform" id="myform" action="<?=current_url()?>" method="post" class="form label-inline uniform" autocomplete="off" >
            <input type="hidden" name="LocationID" id="LocationID" size="10" Value="<?=(isset($LocationID) && $LocationID > 0) ? $LocationID : false?>" />
            <input type="hidden"  name="Del" id="Del" size="1" Value="N" />

    <p>
        <label for="Food">Food Name: </label>  
        <input type="text" name="Food" id="Food" size="30" maxlength="35" />
    </p>

    <p>
        <label for="Calories">Calories: </label>  
        <input type="text" name="Calories" id="Calories" size="10" maxlength="35" />
    </p>
    <p>
        <label for="TotalFat">Total Fat:</label>
        <input type="text" name="TotalFat" id="TotalFat" size="10" maxlength="35" />
    </p>
    <p>
        <label for="Sodium">Sodium:</label>
        <input type="text" name="Sodium" id="Sodium" size="10" maxlength="35" />
    </p>

    <p>
        <label for="DietaryFiber">Dietary Fiber:</label>
        <input type="text" name="DietaryFiber" id="DietaryFiber" size="10" maxlength="35" />
    </p>
    <p>
        <label for="ServingSize">Serving Size:</label>
        <input type="text" name="ServingSize" id="ServingSize" size="10" maxlength="35" />
    </p>
    <p>
        <label for="NumberOfServings">Servings:</label>
        <input type="text" name="NumberOfServings" id="NumberOfServings" size="10" maxlength="35" />
    </p>
    <p>
        <label for="Points">Points:</label>
        <input type="text" name="Points" id="Points" size="10" maxlength="35" />
    </p>
    <p>
        <label for="TotalPoints">Total Points:</label>
        <input type="text" name="TotalPoints" id="TotalPoints" size="10" maxlength="35" />
    </p>

    <div class="buttonrow">
        <? if(isset($TrackID) && $TrackID > 0){ ?>
        <button type="submit" name="submitButton" id="delete" value="delete" class="btn btn-grey FormDeleteBtn"><?=$this->lang->line('delete_btn')?></button>
        <? } ?>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <button type="button" name="submitButton" id="cancel" value="cancel" class="btn btn-black" onclick="javascript:window.location='<?=base_url()?>tracking'"><?=$this->lang->line('cancel_btn')?></button>

        &nbsp;

        <button type="submit" name="submitButton" id="save" value="save" class="btn"><?=$this->lang->line('save_btn')?></button>
    </div>

    <?php echo form_close();?>

我不太确定我做错了什么,所以非常感谢所有的帮助。感谢

0 个答案:

没有答案