php 使用 codeigniter 将数据插入数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15969411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:18:47  来源:igfitidea点击:

insert data into database with codeigniter

phpdatabasecodeigniterinsert

提问by Smudger

Trying to insert a row into my database with CodeIgniter.

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

My database table is Customer_Ordersand the fields are CustomerNameand OrderLines. The variables are being submitted correctly.

我的数据库表是Customer_Orders,字段是CustomerNameOrderLines。变量被正确提交。

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?

关于为什么不的任何想法?

回答by Nilesh Mahajan

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

回答by Mohammad Naim Dahee

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

回答by Avinash vyas

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

回答by Amit Meena

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

回答by Kobus Myburgh

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

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

回答by ram

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

回答by ram

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

回答by smoki

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

回答by sofia

<?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