我正在创建一个cms网站,我遇到了一个问题,我正在尝试修复它,但没有解决,请任何人对此进行指导。我面临的问题是,当我尝试更新记录时,除了一项是多选选项之外,所有内容都将被提取,如果数据库仅包含一个多选选项,则其检索,但是如果它们是两个或其他任何东西数据库中有多个不被检索。请任何人都可以尝试在此方面帮助我。
即时通讯出错
遇到PHP错误严重性:注意
消息:数组到字符串的转换
文件名:models / Contact_model.php
行号:230
回溯:
文件: C:\ xampp \ htdocs \ CodeIgniter_try \ application \ models \ Contact_model.php 行:230功能:_error_handler
文件:C:\ xampp \ htdocs \ CodeIgniter_try \ application \ controllers \ Home.php 行:444功能:Portfolio_update
文件:C:\ xampp \ htdocs \ CodeIgniter_try \ index.php行:315功能: require_once
Controller(Home.php)
MyObject myObject= (MyObject )request.getSession().getAttribute("myObject");
模型(Contact_model.php)
//Updating the Records
public function portfolio_update($id)
{
//$id=$this->input->get('id');
$result['data']=$this->Contact_model->portfolio_byid($id);
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('type[]', 'Type');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('update_portfolio',$result);
}
else
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$title=$this->input->post('title');
$type= $this->input->post('type');
if($this->upload->do_upload('filename'))
{
$fi= $this->upload->data('file_name');
$file = ("uploads/".$result['data'][0]->filename);
//delete the existing file if needed
if (file_exists($file)) {
unlink($file);
}
}
else
{
$fi= $result['data'][0]->filename;
}
$this->Contact_model->portfolio_update($title,$type,$fi,$id);
echo 'Successfully updated your record';
//exit();
}
}
view(update_portfolio.php)
//Updatng the record
function portfolio_update($title,$type,$fi,$id)
{
$query=$this->db->query("update portfolio1 SET first_content='$title',type='$type',filename='$fi' where ID='".$id."'");
}
答案 0 :(得分:1)
实际上,您的类型字段是复选框数组值,所以您不能直接将数组插入数据库中
$type= $this->input->post('type');
这是在服务器端发布时得到的数组,因此需要通过php的内爆函数转换为逗号分隔的字符串,然后再插入数据库。您也可以使用逗号分隔的值来显示选定的值,方法是将它们通过爆炸功能转换为数组。
答案 1 :(得分:0)
根据错误消息,消息:数组到字符串的转换,在文件名:models / Contact_model.php中。但是实际错误的起因是来自控制器。请尝试在以下部分中进行更改:
if($this->upload->do_upload('filename'))
{
$fi= $this->upload->data('file_name');
$file = ("uploads/".$result['data'][0]->filename);
//delete the existing file if needed
if (file_exists($file)) {
unlink($file);
}
}
else
{
$fi= $result['data'][0]->filename;
}
$this->Contact_model->portfolio_update($title,$type,$fi,$id);
echo 'Successfully updated your record';
更改为:
if($this->upload->do_upload('filename'))
{
// $fi= $this->upload->data('file_name'); <-- Array
$get_file = $this->upload->data('file_name');
$fi = $get_file['file_name']; // <-- String
$file = ("uploads/".$result['data'][0]->filename);
if (file_exists($file)) {
unlink($file);
}
}
希望可以帮助您。