我使用CodeIgniter作为我开发的框架。 我试图将数据插入MySQL数据库的表。 数据插入如下。
$dataArray['categoryId'] = $categoryId['id'];
$dataArray['subCategoryId'] = $subTypeId;
$dataArray['title'] = $title;
$dataArray['regionId'] = $regionId;
$dataArray['slogan'] = $slogan;
$dataArray['lastUpdated'] = date('Y-m-d H:i:s');
$dataArray['createdDate'] = date('Y-m-d H:i:s');
$dataArray['state'] = 'Unhide';
$dataArray['status'] = 'Active';
$dataArray['approval'] = 'Incompleted';
$dataArray['user_id'] = $userId;
$this->db->insert($carrentals, $dataArray);
$insertId = $this->db->insert_id();
$ insertId始终返回0并且没有数据插入表中。 我可以手动将数据插入表中。 可能有什么不对? 我有AUTO_INCREMENT列作为id。但是没有插入数据并且总是返回0.
编辑: 我的实际代码是
$dataArray = [
'categoryId' => $categoryId['id'],
'subCategoryId' => $subTypeId,
'title' => $title,
'regionId' => $regionId,
'slogan' => $slogan,
'lastUpdated' =>date('Y-m-d H:i:s')
];
if (empty($_POST['referenceId'])) {
$dataArray['createdDate'] = date('Y-m-d H:i:s');
$dataArray['state'] = 'Unhide';
$dataArray['status'] = 'Active';
$dataArray['approval'] = 'Incompleted';
$dataArray['user_id'] = $userId;
$this->db->insert($carrentals, $dataArray);
$insertId = $this->db->insert_id();
$responseArray = array(
'result' => $insertId,
'success' => true);
return $this->set_response($responseArray, REST_Controller::HTTP_OK);
}
答案 0 :(得分:1)
可能返回FALSE而不是零。 setenv MAIL /usr/spool/mail/$USER
setenv MATLABPATH /home/users/me/matlab/functions:/home/users/me/matlab/scripts
setenv PYTHONPATH /home/users/me/python/modules:/home/users/me/python/scripts
set path=( /sbin /bin /usr/bin /usr/local/bin /usr/local/java/bin
/usr/bin/X11 ~/bin)
if (! $?csh) then
if($?prompt) then
set prmptchr=$prompt
else
set prmptchr="/"
endif
alias setprompt 'set prompt = "$cwd$prmptchr "' # directory name in prompt
alias cd 'chdir \!*; setprompt'
alias pushd 'pushd \!*; setprompt'
alias popd 'popd \!*; setprompt'
setprompt
endif
返回TRUE或FALSE,在继续之前检查结果
insert()
我认为您创建$inserted = $this->db->insert($carrentals, $dataArray);
if($inserted)
{
$responseArray = ['result' => $insertId, 'success' => true];
}
else
{
$responseArray = ['result' => NULL, 'success' => false];
}
return $this->set_response($responseArray, REST_Controller::HTTP_OK);
的方式没有任何问题。
答案 1 :(得分:0)
尝试将 $ dataArray 替换为
$dataArray = array( 'categoryId' => $categoryId['id'],
'subCategoryId' => $subTypeId,
'title' => $title,
'regionId' => $regionId,
'slogan' => $slogan,
'lastUpdated' => date('Y-m-d H:i:s'),
'createdDate' => date('Y-m-d H:i:s'),
'state' => 'Unhide',
'status' => 'Active',
'approval' => 'Incompleted',
'user_id' => $userId
);
$this->db->insert($carrentals, $dataArray);
$insertId = $this->db->insert_id();
答案 2 :(得分:0)
如果$this->db->last_query();
或userId
为空,请尝试检查categoryId
查询的方式,另外检查关系(如果有的话)。
- https://www.codeigniter.com/user_guide/database/helpers.html