14

Trying to insert a row into my database with CodeIgniter.

My database table is Customer_Orders and the fields are CustomerName and OrderLines. The variables are being submitted correctly.

My Controller is( sales.php ):

function new_blank_order_summary() 
  {
      $data = array(
        'OrderLines'=>$this->input->post('orderlines'),
        'CustomerName'=>$this->input->post('customer')
          );
     $this->sales_model->order_summary_insert($data);

    $this->load->view('sales/new_blank_order_summary');
  }

My Model is( sales_model.php ):

function order_summary_insert($data){
    $this->db->insert('Customer_Orders',$data);
}

Whilst the view loads correctly, no data is inserted into the database.

Any ideas as to why not?

3
  • 1
    I just wanted to make sure you know, table names are case sensitive on most unix systems using MySQL. I can only assume, but you have OrderLines and orderlines, CustomerName and customername. Commented Apr 12, 2013 at 10:54
  • Thanks David, I have corrected this but still not working. any other advice. Thanks. Commented Apr 12, 2013 at 11:11
  • where's the view & the table structure? and you'll need to add form_validation into this to make sure you're actually posting anything Commented Apr 12, 2013 at 22:11

9 Answers 9

25

Try this in your model:

function order_summary_insert()
    $OrderLines=$this->input->post('orderlines');
    $CustomerName=$this->input->post('customer');
    $data = array(
        'OrderLines'=>$OrderLines,
        'CustomerName'=>$CustomerName
    );

    $this->db->insert('Customer_Orders',$data);
}

Try to use controller just to control the view and models always post your values in model. it makes easy to understand. Your controller will be:

function new_blank_order_summary() {
    $this->sales_model->order_summary_insert($data);
    $this->load->view('sales/new_blank_order_summary');
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't understand the steps exactly...the $data is defined in model, in the order_summary_insert method...and in controller is given as a parameter for the called method from the model. Sry, I just dont understand this line : $this->sales_model->order_summary_insert($data); is referring to the same $data from the model?
It is not advisable to directly access submission payloads from your model method. By passing values to the model, you maintain an easily testable project. Model methods should never care where their data comes from -- everything should be passed in as parameters for optimal clarity.
2

It will be better for you to write your code like this.

In your Controller Write this code.

    function new_blank_order_summary() {
     $query = $this->sales_model->order_summary_insert();
     if($query) {
        $this->load->view('sales/new_blank_order_summary'); 
    } else {
        $this->load->view('sales/data_insertion_failed');
    }
  }

and in your Model

function order_summary_insert() {
    $orderLines = trim(xss_clean($this->input->post('orderlines')));
    $customerName = trim(xss_clean($this->input->post('customer')));
    $data = array(
        'OrderLines'=>$orderLines,
        'CustomerName'=>$customerName
    );

    $this->db->insert('Customer_Orders',$data);
    return ($this->db->affected_rows() != 1) ? false : true;
}

1 Comment

It is not advisable to directly access submission payloads from your model method. By passing values to the model, you maintain an easily testable project. Model methods should never care where their data comes from -- everything should be passed in as parameters for optimal clarity.
1
function saveProfile(){
    $firstname = $this->input->post('firstname');
    $lastname = $this->input->post('lastname');
    $post_data = array('firstname'=> $firstname,'lastname'=>$lastname);
    $this->db->insert('posts',$post_data);
    return $this->db->insert_id(); 
}

1 Comment

It is not advisable to directly access submission payloads from your model method. By passing values to the model, you maintain an easily testable project. Model methods should never care where their data comes from -- everything should be passed in as parameters for optimal clarity.
1

View

<input type="text" name="name"/>
<input type="text" name="class"/>

Controller

function __construct()
{
    parent:: __construct();
    $this->load->Model('Model');
}

function index()
{
    $this->load->view('view');
}

function user(){
    if (isset($_POST['submit'])){
        $data = array('name'=>$_POST['name'],
                    'class'=>$_POST['class']);
         $this->Model->insert($data);
    }
}

Model

function insert($data)
{
    $this->db->insert('table_name',$data);
    return true;
}

1 Comment

CodeIgniter has dedicated methods for accessing submission data. $_POST/$_REQUEST/$_GET/etc. should not be used.
1

Based on what I see here, you have used lowercase fieldnames in your $data array, and uppercase fieldnames in your database table.

4 Comments

Thanks Kobus, I did change this to be the same case, as per your and Davids advice but still not working. I know the model is being called and run as I can see the echo if I insert one. How can I trouble shoot this or get some feedback from the database? Thanks
Perhaps returning the affected_rows() after inserting the data? return $this->db->affected_rows();
You can find some useful logs in these files. PHP Error Log MySQL Log Apache access and Apache Error log
Affected rows is a bit pointless. If the query doesn't INSERT successfully, an error will be generated. When I design a CodeIgniter model method that auto-increments a primary key, the return value is always the insert_id. This provides the id to the call site in case that record needs to be accessed.
0
function order_summary_insert()
$OrderLines=$this->input->post('orderlines');
$CustomerName=$this->input->post('customer');
$data = array(
'OrderLines'=>$OrderLines,
'CustomerName'=>$CustomerName
);

$this->db->insert('Customer_Orders',$data);
}

1 Comment

There is no explanation with this snippet, but is either a controller which shouldn't be writing to the database or a model that shouldn't be directly accessing submission data.
0

Check your controller:

function order()
    $OrderLines = $this->input->post('orderlines');
    $CustomerName = $this->input->post('customer');
    $data = array(
        'OrderLines' => $OrderLines,
        'CustomerName' =>$CustomerName
    ); 

    $this->db->insert('Customer_Orders', $data);
}

1 Comment

It is not the responsibility of the controller to interact with the database. The submission data should be passed to a model method which writes to the database.
0
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Cnt extends CI_Controller {


 public function insert_view()
 {
  $this->load->view('insert');
 }
 public function insert_data(){
  $name=$this->input->post('emp_name');
  $salary=$this->input->post('emp_salary');
  $arr=array(
   'emp_name'=>$name,
   'emp_salary'=>$salary
   );
  $resp=$this->Model->insert_data('emp1',$arr);
  echo "<script>alert('$resp')</script>";
  $this->insert_view();  
 }
}

for more detail visit: http://wheretodownloadcodeigniter.blogspot.com/2018/04/insert-using-codeigniter.html

2 Comments

Hey, could you provide some explanations in addition? Your shouldn't only show code or/and link, have a look to that link: stackoverflow.com/help/how-to-answer ;)
Single-use variables $name and $salary can be eliminated. post() can receive a whitelist array which will auto-populate missing elements with null defaults.
0

Just insert $this->load->database(); in your model:

function order_summary_insert($data){
    $this->load->database();
    $this->db->insert('Customer_Orders',$data);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.