可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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:
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:
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:
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;
}
回答5:
Based on what I see here, you have used lowercase fieldnames in your $data
array, and uppercase fieldnames in your database table.
回答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:
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:
<?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
回答9:
Just insert $this->load->database();
in your model:
function order_summary_insert($data){
$this->load->database();
$this->db->insert('Customer_Orders',$data);
}