我试图在我的系统中有一个编辑/更新模块。但是,似乎我无法弄清楚为什么我得到数组到字符串转换错误。以下是模型,视图,控制器代码。
模型中的线错误
function updatebyid($userid, $data)
{
$this->db->where('id', $userid); **i get the error in this line in my model**
控制器中的线路错误
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid, $data)) **AND IN THIS LINE TOO IN MY CONTROLLER**
我的模特
function get_user_by_id($id)
{
$this->db->where('id', $id);
$query = $this->db->get('users');
return $query->result();
}
function updatebyid($userid, $data)
{
$this->db->where('id', $userid);
$query = $this->db->update('users', $data);
return $query->result();
//return $query->row()->query;
}
我的观点
</div>
<div class="row well" style="color: black; margin-top: 15px;">
<h2>Edit Profile</h2>
<?php $attributes = array("name" => "registerform");
echo form_open("account/edituserinfo/", $attributes);?>
<div class="form-group">
<div class="col-sm-4">
<label for="fname">First Name</label>
<input class="form-control" name="fname" required type="text" value="<?php echo $fname; ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
<div class="col-sm-4">
<label for="mname">Middle Name</label>
<input class="form-control" name="mname" required placeholder="Middle Name" type="text" value="<?php echo $mname; ?>" />
<span class="text-danger"><?php echo form_error('mname'); ?></span>
</div>
<div class="col-sm-4">
<label for="lname">Last Name</label>
<input class="form-control" name="lname" required placeholder="Last Name" type="text" value="<?php echo $lname; ?>" />
<span class="text-danger"><?php echo form_error('lname'); ?></span>
</div>
</div>
<div class="input-group" style="padding-top: 15px; margin-left: 15px">
<button name="submit" type="submit" class="btn btn-success">Update</button>
<!--input class="form-control" name="id" placeholder="id" type="hidden" value="<?php echo $id; ?>" /-->
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
我的控制器
function index()
{
$this->load->view('include/headnav');
$details = $this->model_user->get_user_by_id($this->session->userdata('id'));
$data['name'] = $details[0]->fname . " " . $details[0]->lname;
$data['level'] = $this->session->userdata('level');
$data['fname'] = $details[0]->fname;
$data['mname'] = $details[0]->mname;
$data['lname'] = $details[0]->lname;
$data['gender'] = $details[0]->gender;
$data['email'] = $details[0]->email;
$data['mobileNum'] = $details[0]->mobileNum;
$data['landlineNum'] = $details[0]->landlineNum;
$data['homeaddress'] = $details[0]->homeaddress;
//$data['position'] = $details[0]->position;
//$data['institution'] = $details[0]->institution;
//$data['institutionAddr'] = $details[0]->institutionAddr;
//$data['institutionNum'] = $details[0]->institutionNum;
$this->load->view('view_account', $data);
$this->load->view('include/footernav');
}
function edituserinfo()
{
$this->load->view('include/headnav');
$data = array(
'lname' => $this->input->post('lname'),
'fname' => $this->input->post('fname'),
'mname' => $this->input->post('mname'),
'gender' => $this->input->post('gender'),
'homeaddress' => $this->input->post('homeaddress'),
'mobileNum' => $this->input->post('mobileNum'),
//'password' => $this->input->post('password'),
//'cPassword' => $this->input->post('cPassword'),
//'institution' => $this->input->post('institution'),
//'institutionAddr' => $this->input->post('institutionAddr'),
//'institutionNum' => $this->input->post('institutionNum'),
//'position' => $this->input->post('position'),
);
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid, $data))
{
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Profile edited.</div>');
redirect('account/edituserinfo');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Error. Check credentials.</div>');
redirect('account/edituserinfo');
}
}
function updateuinfoview()
{
$this->load->view('include/headnav');
$details = $this->model_user->get_user_by_id($this->session->userdata('id'));
//$data['fname'] = $details[0]->fname . " " . $details[0]->lname;
$data['fname'] = $details[0]->fname;
$data['lname'] = $details[0]->lname;
$data['mname'] = $details[0]->mname;
$data['gender'] = $details[0]->gender;
$data['email'] = $details[0]->email;
$data['mobileNum'] = $details[0]->mobileNum;
$data['landlineNum'] = $details[0]->landlineNum;
$data['homeaddress'] = $details[0]->homeaddress;
//$data['position'] = $details[0]->position;
//$data['institution'] = $details[0]->institution;
//$data['institutionAddr'] = $details[0]->institutionAddr;
//$data['institutionNum'] = $details[0]->institutionNum;
$this->load->view('view_editprofile', $data);
$this->load->view('include/footernav');
}
这是错误消息
遇到PHP错误 严重性:注意 消息:数组到字符串转换 文件名:database / DB_query_builder.php 行号:669
回溯: 文件:C:\ xampp \ htdocs \ THTF.6 \ application \ models \ model_user.php 行:73 功能:在哪里 文件:C:\ xampp \ htdocs \ THTF.6 \ application \ controllers \ Account.php 行:74 功能:updatebyid 文件:C:\ xampp \ htdocs \ THTF.6 \ index.php 行:315 功能:require_once
发生数据库错误 错误号码:1054 未知列&#39;数组&#39;在&#39; where子句&#39; 更新
的地方users
设置lname
=&#39; Taz&#39;,fname
=&#39; Vinny&#39;,mname
=&#39; Paz&#39 ;,gender
=&#39;男&#39;,homeaddress
=&#39;美国&#39;,mobileNum
=&#39; 4123&#39;在id
=Array
文件名:C:/xampp/htdocs/THTF.6/system/database/DB_driver.php 行号:691
答案 0 :(得分:1)
您正在获得数组到字符串转换错误,因为您的模型:
$this->model_user->get_user_by_id($this->session->userdata('id'))
返回一个对象数组。要解决您的问题,您可以循环结果 或者如果您只需要结果的第一行,您可以这样做:
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid[0]->id, $data))
或者您可以像这样修改模型:
function get_user_by_id($id)
{
$this->db->where('id', $id);
$query = $this->db->get('users');
return $query->row(); // use row if you only want the first row of the result of your query
}
<强>控制器强>:
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid->id, $data))
*************** ****************************************
或者更全面的模型,这样您就不需要更换控制器了:
function get_user_by_id($id)
{
$this->db->where('id', $id);
$query = $this->db->get('users');
$result = $query->row();
return ($result) ? $result->id : false;
}
答案 1 :(得分:0)
您正在更新
中传递用户详细信息数组而不是用户ID更改此行
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
到
$userid = $this->session->userdata('id');
在控制器的function edituserinfo()
中
答案 2 :(得分:0)
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
在上面一行中,您将查询将返回数组的数据库。
而是将用户ID设置为
$userid = $this->session->userdata('id');
你的答案