dswqw66280 2013-06-11 11:31
浏览 50
已采纳

PHP / MySQL,如何只更新数据库中的某些记录,而不是一次更新?

Can anyone tell me how not to update all my records in table. This is my update statement.

public function update() {

    try {
        $form = new Form();

        $form   ->post('id')
                ->post('username')
                ->val('minlenght', 4)
                ->val('maxlenght', 20)
                ->post('role')
                ->post('first_name')
                ->post('last_name')
                ->post('birthdate')
                ->post('email')
                ->val('email');

        $form->submit();

        // sett variables after  
        $this->model->id = $_POST['id'];
        $this->model->username = $_POST['username'];
        $this->model->pass = $_POST['pass'];
        $this->model->role = $_POST['role'];
        $this->model->first_name = $_POST['first_name'];
        $this->model->last_name = $_POST['last_name'];
        $this->model->email = $_POST['email'];
        $this->model->join_date = $_POST['join_date'];
        $this->model->birthdate = $_POST['birthdate'];
        $this->model->country = $_POST['country'];
        $this->model->status = $_POST['status'];

        // here i call the method from model
        $this->model->update();

        $_SESSION['message'] = '<span class="label label-success">Saved</span>';
        redirect_to(URL."user/edit/".$this->model->id);
    } catch (Exception $e) {
        $_SESSION['message'] = '<span class="label label-important">' . $e->getMessage() . '</span>';
        redirect_to(URL . 'user?page=1');
    }

}

Here is the code for the model.

public function update() {

    $attributes = $this->sanitized_attributes();
    $attribute_pairs = array();
    foreach ($attributes as $key => $value) {
        $attribute_pairs[] = "{$key}='{$value}'";
    }
    $sql = "UPDATE " . self::$table_name . " SET ";
    $sql .= join(", ", $attribute_pairs);
    $sql .= " WHERE id=" . $this->database->escape_value($this->id);
    $this->database->query($sql);
    return ($this->database->affected_rows() == 1) ? true : false;
}

The attributes are

private static $db_fields = array('id', 'role', 'username', 'pass', 'role', 'first_name', 'last_name', 'email', 'join_date', 'birthdate', 'country', 'avatar', 'status');
private static $table_name = "user";
public $id;
public $username;
public $pass;
public $role;
public $first_name;
public $last_name;
public $email;
public $join_date;
public $birthdate;
public $country;
public $avatar;
public $status;

When I try to update, even if I put only username, it updates all records in database. What need to be change in this code so that I can update only records I specify? I tried couple of hours to try to figure it out, but I came at the conclusion that I need to remake my user class, not to be so automatic.

  • 写回答

1条回答 默认 最新

  • dongmangwei3822 2013-06-11 11:37
    关注

    Here is the updated version of the update() method that you have listed above

    public function update() {
    
        $attributes = $this->sanitized_attributes();
        $attribute_pairs = array();
        foreach ($attributes as $key => $value) {
    if(empty($value))
    {
    continue;
    }
    else
    {
    $attribute_pairs[] = "{$key}='{$value}'";
    }
    
        }
        $sql = "UPDATE " . self::$table_name . " SET ";
        $sql .= join(", ", $attribute_pairs);
        $sql .= " WHERE id=" . $this->database->escape_value($this->id);
        $this->database->query($sql);
        return ($this->database->affected_rows() == 1) ? true : false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题