使用codeigniter将数据插入数据库

时间:2022-10-06 16:09:30

Trying to insert a row into my database with CodeIgniter.

尝试使用CodeIgniter在我的数据库中插入一行。

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

我的数据库表是Customer_Orders,字段是CustomerName和OrderLines。变量正在正确提交。

My Controller is( sales.php ):

我的控制器是(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 ):

我的模型是(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?

任何想法为什么不呢?

9 个解决方案

#1


8  

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');
}

#2


1  

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;
}

#3


1  

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

根据我在这里看到的,您在$ data数组中使用了小写字段名,在数据库表中使用了大写字段名。

#4


0  

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(); 
}

#5


0  

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;
}

#6


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);
}

#7


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);
}

#8


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

有关更多详细信息,请访问:http://wheretodownloadcodeigniter.blogspot.com/2018/04/insert-using-codeigniter.html

#9


0  

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

只需插入$ this-> load-> database();在你的模型中:

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

#1


8  

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');
}

#2


1  

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;
}

#3


1  

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

根据我在这里看到的,您在$ data数组中使用了小写字段名,在数据库表中使用了大写字段名。

#4


0  

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(); 
}

#5


0  

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;
}

#6


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);
}

#7


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);
}

#8


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

有关更多详细信息,请访问:http://wheretodownloadcodeigniter.blogspot.com/2018/04/insert-using-codeigniter.html

#9


0  

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

只需插入$ this-> load-> database();在你的模型中:

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