gpt4 book ai didi

php - CodeIgniter insert_batch : Field 'exp_user' doesn't have a default value

转载 作者:行者123 更新时间:2023-11-29 07:10:03 28 4
gpt4 key购买 nike

我正在尝试将数组作为一行插入到数据库上的“费用”表中:

public function insert_expense($expenses){
$this->db->insert_batch('expenses', $expenses);
}

我不断收到此错误:

A Database Error Occurred
Error Number: 1364
Field 'exp_user' doesn't have a default value
INSERT INTO expenses () VALUES (), (), (), (), (), (), (), (), (), (), (), ()
Filename: models/Expenses_model.php
Line Number: 26

这是我的表的结构:

enter image description here

这是我尝试插入的示例:

array(12) {
["exp_user"] "1"
["exp_date"] "2016-10-18"
["exp_date_request"] "2016-10-18"
["exp_client"] "Potato"
["exp_provider"] "Miew"
["exp_amount"] "1"
["exp_currency"] "₪"
["exp_budget"] "budget2"
["exp_method"] "cc"
["exp_frequency"] "Monthly"
["exp_expenditure"] "asdasdas asdas dsa as"
["exp_charge_client"] NULL
}

最佳答案

如果变量 $expenses 是您示例中的数组:

$expenses = array(
"exp_user" => "1",
"exp_date" => "2016-10-18",
"exp_date_request" => "2016-10-18",
"exp_client" => "Potato",
"exp_provider" => "Miew",
"exp_amount" => "1",
"exp_currency" => "₪",
"exp_budget" => "budget2",
"exp_method" => "cc",
"exp_frequency" => "Monthly",
"exp_expenditure" => "asdasdas asdas dsa as",
"exp_charge_client" => NULL
);

您不能在 insert_batch 函数中使用它(因为 insert_batch 函数期望获取(数据)数组作为第二个参数)。

基本上,insert_batch 函数使您能够插入多行(无需循环 insert 函数)。您可以使用多维数组来实现:

$expenses = array(
array(
"exp_user" => "1",
"exp_date" => "2016-10-18",
"exp_date_request" => "2016-10-18",
"exp_client" => "Potato",
"exp_provider" => "Miew",
"exp_amount" => "1",
"exp_currency" => "₪",
"exp_budget" => "budget2",
"exp_method" => "cc",
"exp_frequency" => "Monthly",
"exp_expenditure" => "asdasdas asdas dsa as",
"exp_charge_client" => NULL
),
array(
"exp_user" => "1",
"exp_date" => "2016-10-18",
"exp_date_request" => "2016-10-18",
"exp_client" => "Potato",
"exp_provider" => "Miew",
"exp_amount" => "1",
"exp_currency" => "₪",
"exp_budget" => "budget2",
"exp_method" => "cc",
"exp_frequency" => "Monthly",
"exp_expenditure" => "asdasdas asdas dsa as",
"exp_charge_client" => NULL
),
array(
"exp_user" => "1",
"exp_date" => "2016-10-18",
"exp_date_request" => "2016-10-18",
"exp_client" => "Potato",
"exp_provider" => "Miew",
"exp_amount" => "1",
"exp_currency" => "₪",
"exp_budget" => "budget2",
"exp_method" => "cc",
"exp_frequency" => "Monthly",
"exp_expenditure" => "asdasdas asdas dsa as",
"exp_charge_client" => NULL
)
);

现在使用 insert_batch 是有意义的。

如果您只想插入 1 条记录,则可以使用 insert 函数:

$this->db->insert('expenses', $expenses); 

或者将调用更改为:

$this->db->insert_batch('expenses', array( $expenses ) ); 

关于php - CodeIgniter insert_batch : Field 'exp_user' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40112404/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com