CodeIgniter Forums
insert not working. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: insert not working. (/thread-52446.html)

Pages: 1 2


insert not working. - El Forum - 06-11-2012

[eluser]reghan[/eluser]
Hello the following is my code I am trying to insert a new system but it doesnt seem to want to insert. I am fairly new to this program, so it might be something simple but at this time I am just stuck. Any help would be great

Here is my Model:
Code:
<?php
class Department_model extends CI_Model {

public function __construct()
{
  $this->load->database();
}

  function systems_table_list()
{
  $this->db->order_by('SystemID', 'asc');
  return $this->db->get('systems');
}


// add new system
function saveSystem($system)
{

  $this->db->insert('systems', $system);
  return $this->db->insert_id();

  }




}

here is the controller:

Code:
function addSystem()
{
  
  
  // set common properties
  $data['title'] = 'Add new system';
  $data['action'] = site_url('bia/addSystem');
  $data['link_back'] = anchor('bia/systems/','Back to list of persons',array('class'=>'back'));
  
  //set validation rules
  //have validation set here but it is too long to include its the same as the function below
  
  
   if ($this->form_validation->run() == FALSE)
   {
  
              $data['message'] = '';
   }
  
  else
  {
   // save data
   $system = array('SystemName' => $this->input->post('sysname'),
     'Servers' => $this->input->post('server'),
     'HA' => $this->input->post('ha'),
     'DR' => $this->input->post('dr'),
     'Desc' => $this->input->post('descrip'),
     'Class' => $this->input->post('class'),
     'LowestRTO' => $this->input->post('lowRTO'),
     'LowestRTOProcess' => $this->input->post('lowProcess'),
     'LowestRTODept' => $this->input->post('lowDept'));
  
  
     $this->department_model->saveSystem($system);
    // set user message
   $data['message'] = '<div class="success">add new person success</div>';
   }
  
  
  
  
   // load view
  $this->load->view('systemEdit', $data);

}

function add_sys(){


         // set common properties

         $data['title'] = 'Add New System';



  //set validation rules
  $this->form_validation->set_rules('sysname', 'System Name', 'required');
  $this->form_validation->set_rules('server','Server Name', 'required');
  $this->form_validation->set_rules('ha', 'HA','required');
  $this->form_validation->set_rules('dr', 'DR','required');
  $this->form_validation->set_rules('descrip','Description', 'required');
  $this->form_validation->set_rules('class','Class', 'required');
  $this->form_validation->set_rules('lowRTO','Lowest RTO', 'required|numeric');
  $this->form_validation->set_rules('lowProcess','Lowest RTO Process', 'required');
  $this->form_validation->set_rules('lowDept','Lowest RTO Department', 'required');

// set common properties
   $data['title'] = 'Add a new system';
   $data['message'] = '';
   $data['action'] = site_url('bia/addSystem');
   $data['link_back'] = anchor('bia/systems/','Back to list of systems',array('class'=>'back'));

   //load the add system view.
   $this->load->view('systemEdit', $data);

     }
view:
Code:
<div class="content">
  <h1>&lt;?php echo $title; ?&gt;</h1>
  <p>&lt;?php echo $message; ?&gt;</p>

&lt;form method="post" action="&lt;?php echo $action; ?&gt;"&gt;

   <div class="data">
<table>
   <tr>

                 <td valign="top"><strong>System Name: </strong><span>*</span></td>

                 <td>
                  &lt;input type="text" name="sysname" size="60" class="text" value="&lt;?php echo set_value('sysname'); ?&gt;"/&gt;
     &lt;?php echo form_error('sysname'); ?&gt;
    </td>

          </tr>
  <tr>

                 <td valign="top"><strong>Server: </strong><span>*</span></td>
    <td>&lt;input type="text"  name="server" size="60" value="&lt;?php echo set_value('server'); ?&gt;" /&gt;&lt;br /></td>
   <td>&lt;?php echo form_error('server'); ?&gt;</td>
             </tr>
   <tr>

                 <td valign="top"><strong>HA: </strong><span>*</span></td>
   <td>

   <select name="ha">
       <option value="Yes">Yes</option>
       <option value="Partial">Partial</option>
       <option value="None">None</option>
       <option value="Na">NA</option>
       </select><br />
      </td>
                
                
            </tr>
            <tr>
              
              <td valign="top"><strong>DR: </strong><span>*</span></td>
       <td>
    
       <select name="ha">
           <option value="Yes">Yes</option>
           <option value="Partial">Partial</option>
           <option value="None">None</option>
           <option value="Na">NA</option>
          </select><br />
      </td>
            
            </tr>
            
            <tr>
              
              <td valign="top"><strong>Description: </strong><span>*</span></td>
              <td>&lt;textarea name="descrip" class="text" cols="45" rows="10" &gt;&lt;?php echo set_value('descrip'); ?&gt;&lt;/textarea&gt;&lt;/td>
   <td>&lt;?php echo form_error('lowProcess'); ?&gt;</td>
            
            </tr>
            
            <tr>  
              <td valign="top"><strong>Class: </strong><span>*</span></td>
              <td>
  
    <select name="class">
     <option value="Platform-Network">Platform-Network</option>
     <option value="Platform-Server">Platform-Server</option>
     <option value="Platform-Storage">Platform-Storage</option>
     <option value="Application">Application</option>
     <option value="Infrastructure Service">Infrastructure Service</option>
    </select><br />
    </td>

some of this code from the view is cut off as it doesn fit, but I tried to include important info. But if you have any suggestions why it is not doing an insert please let me know that would be super great!!!!


insert not working. - El Forum - 06-11-2012

[eluser]CroNiX[/eluser]
Code:
public function __construct()
{
  parent::__construct(); //Always point the construct back to the parent class, IF you are doing something in the construct (you're loading the db here, so it's necessary)
  $this->load->database();
}



insert not working. - El Forum - 06-11-2012

[eluser]reghan[/eluser]
Hello.

I have tried that and it still does not work. its getting stuck at this form validation:

Code:
if ($this->form_validation->run() == FALSE)
   {
  
              $data['message'] = 'it is going here.ERROR NOT INSERTING';
   }
  
  else
  {
   // save data
   $system = array('SystemName' => $this->input->post('sysname'),
     'Servers' => $this->input->post('server'),
     'HA' => $this->input->post('ha'),
     'DR' => $this->input->post('dr'),
     'Desc' => $this->input->post('descrip'),
     'Class' => $this->input->post('class'),
     'LowestRTO' => $this->input->post('lowRTO'),
     'LowestRTOProcess' => $this->input->post('lowProcess'),
     'LowestRTODept' => $this->input->post('lowDept'));
  
  
     $this->department_model->saveSystem($system);
    // set user message
   $data['message'] = '<div class="success">add new person success</div>';
   }

The message in my view is that ERROR NOT INSERTING, so for some reason its returning a false validation. THis is what I am stuck on and unsure why...or what I need to change in my code.

Thanks again!


insert not working. - El Forum - 06-11-2012

[eluser]CroNiX[/eluser]
Regardless, it needs to be there.

Try looking at the validation errors then?
Code:
if ($this->form_validation->run() == FALSE)
{
  $data['message'] = validation_errors();
}



insert not working. - El Forum - 06-11-2012

[eluser]reghan[/eluser]
ok sounds good. Ill leave that in there. I will def try that valication_errors to see whats triggering it. Thanks!


insert not working. - El Forum - 06-11-2012

[eluser]reghan[/eluser]
I did this an I am getting a * required for the validation_errors.

Code:
//set validation rules
  $this->form_validation->set_rules('sysname', 'System Name', 'required');
  $this->form_validation->set_rules('server','Server Name', 'required');
  $this->form_validation->set_rules('ha', 'HA','required');
  $this->form_validation->set_rules('dr', 'DR','required');
  $this->form_validation->set_rules('descrip','Description', 'required');
  $this->form_validation->set_rules('class','Class', 'required');
  $this->form_validation->set_rules('lowRTO','Lowest RTO', 'required|numeric');
  $this->form_validation->set_rules('lowProcess','Lowest RTO Process', 'required');
  $this->form_validation->set_rules('lowDept','Lowest RTO Department', 'required');
  
  //set rule messages
  $this->form_validation->set_message('required', '* required');
  $this->form_validation->set_message('numeric', '* Must be numeric');

I am feeling a little silly but I still cannot figure it out. This is my error validation code above.

Any thought!?!?
Smile


insert not working. - El Forum - 06-11-2012

[eluser]CroNiX[/eluser]
and you put a value in all required fields, submitted the form and get that error?


insert not working. - El Forum - 06-11-2012

[eluser]reghan[/eluser]
yes that is correct. I have info in every field and it returns * required


insert not working. - El Forum - 06-11-2012

[eluser]reghan[/eluser]
Code:
function add_sys(){


         // set common properties

         $data['title'] = 'Add New System';



  



  
  // set common properties
   $data['title'] = 'Add a new system';
   $data['message'] = '';
   $data['action'] = site_url('bia/addSystem');
   $data['link_back'] = anchor('bia/systems/','Back to list of systems',array('class'=>'back'));

   //load the add system view.
   $this->load->view('systemEdit', $data);

     }

function addSystem()
{
  
  
  // set common properties
  $data['title'] = 'Add new system';
  $data['action'] = site_url('bia/addSystem');
  $data['link_back'] = anchor('bia/systems/','Back to list of persons',array('class'=>'back'));
  
  //set validation rules
  $this->form_validation->set_rules('sysname', 'System Name', 'required');
  $this->form_validation->set_rules('server','Server Name', 'required');
  $this->form_validation->set_rules('ha', 'HA','required');
  $this->form_validation->set_rules('dr', 'DR','required');
  $this->form_validation->set_rules('descrip','Description', 'required');
  $this->form_validation->set_rules('class','Class', 'required');
  $this->form_validation->set_rules('lowRTO','Lowest RTO', 'required|numeric');
  $this->form_validation->set_rules('lowProcess','Lowest RTO Process', 'required');
  $this->form_validation->set_rules('lowDept','Lowest RTO Department', 'required');
  
  //set rule messages
  $this->form_validation->set_message('required', '* required');
  $this->form_validation->set_message('numeric', '* Must be numeric');
  
  
   if ($this->form_validation->run() == FALSE)
   {
  
              $data['message'] = validation_errors();
   }
  
  else
  {
   // save data
   $system = array('SystemName' => $this->input->post('sysname'),
     'Servers' => $this->input->post('server'),
     'HA' => $this->input->post('ha'),
     'DR' => $this->input->post('dr'),
     'Desc' => $this->input->post('descrip'),
     'Class' => $this->input->post('class'),
     'LowestRTO' => $this->input->post('lowRTO'),
     'LowestRTOProcess' => $this->input->post('lowProcess'),
     'LowestRTODept' => $this->input->post('lowDept'));
  
  
     $this->department_model->saveSystem($system);
    // set user message
   $data['message'] = '<div class="success">add new person success</div>';
   }
  
  
  
  
   // load view
  $this->load->view('systemEdit', $data);


  
}



insert not working. - El Forum - 06-11-2012

[eluser]CroNiX[/eluser]
make sure all of your rule names match your field names exactly. "sysname", "server", etc.

For now until you get it working, remove your custom error messages (set_message()) so that it will show you the field(s) that are having the problem. It's hard to narrow it down when you are hiding the field names from the error messages.