0

私は 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();?>

何が間違っているのかよくわからないので、すべての助けをいただければ幸いです。ありがとう

4

0 に答える 0