0

谁能告诉我如何不更新表中的所有记录。这是我的更新声明。

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');
    }

}

这是模型的代码。

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;
}

属性是

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;

当我尝试更新时,即使我只输入用户名,它也会更新数据库中的所有记录。此代码需要更改哪些内容才能仅更新我指定的记录?我尝试了几个小时试图弄清楚,但我得出的结论是我需要重新制作我的user class,而不是那么自动。

4

1 回答 1

1

这是您上面列出的 update() 方法的更新版本

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;
}
于 2013-06-11T11:37:52.787 回答