Php 在Codeigniter上使用更新查询时如何忽略一个字段?
这是我网站的一个用户模型Php 在Codeigniter上使用更新查询时如何忽略一个字段?,php,codeigniter,query-builder,Php,Codeigniter,Query Builder,这是我网站的一个用户模型 class User_model extends CI_Model { public $user_first_name; public $user_last_name; public $user_email; public $user_password; public $ur_id; public function insert_entry()
class User_model extends CI_Model {
public $user_first_name;
public $user_last_name;
public $user_email;
public $user_password;
public $ur_id;
public function insert_entry()
{
$password = password_hash($this->input->post('user_password'), PASSWORD_DEFAULT);
$this->user_first_name = $this->input->post('user_first_name');
$this->user_last_name = $this->input->post('user_last_name');
$this->user_email = $this->input->post('user_email');
$this->user_password = $password;
$this->ur_id = $this->input->post('ur_id');
$this->db->insert('user', $this);
return $this->db->affected_rows();
}
public function update_entry()
{
$this->user_first_name = $this->input->post('user_first_name');
$this->user_last_name = $this->input->post('user_last_name');
$this->user_email = $this->input->post('user_email');
$this->ur_id = $this->input->post('ur_id');
$this->db->update('user', $this, array('user_id' => $this->input->post('user_id')));
return $this->db->affected_rows();
}
}
正如你在代码中看到的,我不会在编辑时更新密码。因此,我已将其从update\u entry()
函数中删除
当我调用update\u entry()
函数时,它返回以下查询
UPDATE `user` SET `user_first_name` = 'Ruwan', `user_last_name` = 'Thilanka', `user_email` = 'ruwan1@example.com', `user_password` = NULL, `ur_id` = '2'
WHERE `user_id` = '7'
如您所见,它自动将'user\u password'=NULL
添加到查询中
为什么它是自动添加的,即使我在
update\u entry()
中没有提到它。我如何移除它?(请不要让它出现在insert\u entry()
函数中。为什么要在update\u query()中更新用户ID
我建议您在SQL查询中添加WHERE,在WHERE之后提供用户ID,然后更新用户表
下面是一个在我的系统中运行的函数示例。您可能会发现它很有用
确保我在数据库中使用的用户id字段名相同,否则
使用用户id字段更改它
public function update_entry()
{
$postedData = $this->input->post();
$userID = $postedData['user_id'];
$refinePostedData = array(
'your-field-name' => $postedData['user_first_name'],
'your-field-name' => $postedData['user_last_name'],
'your-field-name' => $postedData['user_email'],
);
if($refinePostedData!='')
{
$this->db->where("id", $userID);
$this->db->update("user", $refinePostedData);
return $this->db->affected_rows();
}
}
为什么要在update_query()中更新用户ID 我建议您在SQL查询中添加WHERE,在WHERE之后提供用户ID,然后更新用户表 下面是一个在我的系统中运行的函数示例。您可能会发现它很有用 确保我在数据库中使用的用户id字段名相同,否则 使用用户id字段更改它
public function update_entry()
{
$postedData = $this->input->post();
$userID = $postedData['user_id'];
$refinePostedData = array(
'your-field-name' => $postedData['user_first_name'],
'your-field-name' => $postedData['user_last_name'],
'your-field-name' => $postedData['user_email'],
);
if($refinePostedData!='')
{
$this->db->where("id", $userID);
$this->db->update("user", $refinePostedData);
return $this->db->affected_rows();
}
}
这里我们使用unset()fn从“$this”对象中删除“user_password”字段,并将数据传递给更新
public function update_entry()
{
$this->user_first_name = $this->input->post('user_first_name');
$this->user_last_name = $this->input->post('user_last_name');
$this->user_email = $this->input->post('user_email');
$this->ur_id = $this->input->post('ur_id');
unset($this->user_password);
$this->db->update('user', $this, array('user_id' => $this->input->post('user_id')));
return $this->db->affected_rows();
}
这里我们使用unset()fn从“$this”对象中删除“user_password”字段,并将数据传递给更新
public function update_entry()
{
$this->user_first_name = $this->input->post('user_first_name');
$this->user_last_name = $this->input->post('user_last_name');
$this->user_email = $this->input->post('user_email');
$this->ur_id = $this->input->post('ur_id');
unset($this->user_password);
$this->db->update('user', $this, array('user_id' => $this->input->post('user_id')));
return $this->db->affected_rows();
}