CodeIgniter Forums
Cannot information from my form to my database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Cannot information from my form to my database (/showthread.php?tid=58714)



Cannot information from my form to my database - El Forum - 07-11-2013

[eluser]Unknown[/eluser]
I am trying to put together a package that will allow me to record my calories and other information into a database. I can get the database to acknowledge the id and update it but I can not get the information from the form to the database. Here is my form
Code:
<body>
  <h2>Enter Food information</h2>
  &lt;?php echo form_open('tracking/add');?&gt;
  
  <p>
   <label for="Food">Food Name: </label>  
   &lt;input type="text" name="Food" id="Food" size="30" maxlength="35" /&gt;
  </p>
  
  <p>
   <label for="Calories">Calories: </label>  
   &lt;input type="text" name="Calories" id="Calories" size="10" maxlength="35" /&gt;
  </p>
  <p>
   <label for="TotalFat">Total Fat:</label>
   &lt;input type="text" name="TotalFat" id="TotalFat" size="10" maxlength="35" /&gt;
  </p>
  <p>
   <label for="Sodium">Sodium:</label>
   &lt;input type="text" name="Sodium" id="Sodium" size="10" maxlength="35" /&gt;
  </p>
  
  <p>
   <label for="Carbs">Carbs:</label>  
   &lt;input type="text" name="Carbs" id="Carbs" size="10" maxlength="35" /&gt;
  </p>
  
  
  <p>
   <label for="DietaryFiber">Dietary Fiber:</label>
   &lt;input type="text" name="DietaryFiber" id="DietaryFiber" size="10" maxlength="35" /&gt;
  </p>
  <p>
   <label for="Sugar">Sugars:</label>
   &lt;input type="text" name="Sugar" id="Sugar" size="10" maxlength="35" /&gt;
  </p>
  
  <p>
   <label for="Protein">Protein:</label>
   &lt;input type="text" name="Protein" id="Protein" size="10" maxlength="35" /&gt;
  </p>
  <p>
   <label for="ServingSize">Serving Size:</label>
   &lt;input type="text" name="ServingSize" id="ServingSize" size="10" maxlength="35" /&gt;
  </p>
  <p>
   <label for="NumberOfServings">Servings:</label>
   &lt;input type="text" name="NumberOfServings" id="NumberOfServings" size="10" maxlength="35" /&gt;
  </p>
  <p>
   <label for="TotalPoints">Total Points:</label>
   &lt;input type="text" name="TotalPoints" id="TotalPoints" size="10" maxlength="35" /&gt;
  </p>
  
  <p>
   <div id="submit">
   &lt;input type="submit" value="Submit" /&gt;
   </div>
  </p>
  
  &lt;?php echo form_close();?&gt;
&lt;/body&gt;
&lt;/html&gt;

And here is my controller I am using the add function to add the information to the database.
Code:
function index()
  {
   $this->Data['TrackingArray'] = (array)$this->tracking_model->getTracks();
   $this->load->view('tracking_view', $this->Data);
        }
  
  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);
   }
  }

I also added a validation to the controller. Here is my model and the add function.
Code:
function addTracks()
{
  
  $Data['Food'] = $this->input->post('Food');
  $Data['Calories'] = $this->input->post('Calories');
  $Data['Carbs'] = $this->input->post('Carbs');
  $Data['TotalFat'] = $this->input->post('TotalFat');
  $Data['DietaryFiber'] = $this->input->post('DietaryFiber');
  $Data['Protein'] = $this->input->post('Protein');
  $Data['Sodium'] = $this->input->post('Sodium');
  $Data['Sugar'] = $this->input->post('Sugar');
  $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;
  }
}

I appreciate any help with getting this to work. Thank you in advance.