【问题标题】:insert data into database with codeigniter使用codeigniter将数据插入数据库
【发布时间】:2013-04-04 20:46:44
【问题描述】:

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

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

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

我的模型是(sales_model.php):

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

虽然视图加载正确,但没有数据插入到数据库中。

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

【问题讨论】:

  • 我只是想确保您知道,在大多数使用 MySQL 的 unix 系统上,表名是区分大小写的。我只能假设,但您有 OrderLines 和 orderlines、CustomerName 和 customername。
  • 感谢大卫,我已更正此问题,但仍无法正常工作。任何其他建议。谢谢。
  • 视图和表结构在哪里?您需要在其中添加 form_validation 以确保您实际发布任何内容

标签: php database codeigniter insert


【解决方案1】:

在你的模型中试试这个:

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

尝试使用控制器来控制视图,模型总是在模型中发布你的值。它很容易理解。 您的控制器将是:

function new_blank_order_summary() {
    $this->sales_model->order_summary_insert($data);
    $this->load->view('sales/new_blank_order_summary');
}

【讨论】:

    【解决方案2】:

    这样写你的代码会更好。

    在你的控制器中写下这段代码。

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

    在你的模型中

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

      【讨论】:

        【解决方案4】:

        查看

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

        控制器

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

        型号

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • 感谢 Kobus,根据您和 Davids 的建议,我确实将其更改为相同的情况,但仍然无法正常工作。我知道模型正在被调用并运行,因为如果我插入一个,我可以看到回声。我怎样才能解决这个问题或从数据库中获得一些反馈?谢谢
          • 插入数据后可能返回affected_rows()? return $this-&gt;db-&gt;affected_rows();
          • 您可以在这些文件中找到一些有用的日志。 PHP 错误日志 MySQL 日志 Apache 访问和 Apache 错误日志
          【解决方案6】:
          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】:

            检查你的控制器:

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

              更多详情请访问: http://wheretodownloadcodeigniter.blogspot.com/2018/04/insert-using-codeigniter.html

              【讨论】:

              【解决方案9】:

              只需在您的模型中插入$this-&gt;load-&gt;database();

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

              【讨论】:

                猜你喜欢
                • 2016-09-18
                • 1970-01-01
                • 2014-05-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-08-04
                相关资源
                最近更新 更多