如何更新多个字段


How to update multiple fields?

我收到许多错误,与未定义profile_address有关。当前代码的大部分来自 1 个值的工作更新,此后我尝试添加更新第二个值,我想扩展它以更新另外 10 个值。

我认为问题是变量在我的应用程序中没有正确解析。

我最初尝试将其添加到模型中所示的数组中,但这不起作用。

我认为这是一个相当微不足道的问题,我相信这是一个我还没有想到的简单解决方案。

视图:

<form action="<?php echo Config::get('URL'); ?>login/editUserProfile_action" method="post">
    <label for="comment">Name</label>
    <textarea class="form-control" rows="3" name="profile_name"></textarea>
    <br>
    <label for="comment">Address</label>
    <textarea class="form-control" rows="3" name="profile_address"></textarea>
</form>

控制器:

    public function editUserProfile()
{
    Auth::checkAuthentication();
    $this->View->render('login/editUserProfile');
}
/**
 * Edit user profile (perform the real action after form has been submitted)
 * Auth::checkAuthentication() makes sure that only logged in users can use this action and see this page
 */
// make this POST
public function editUserProfile_action()
{
    Auth::checkAuthentication();
    UserModel::editUserProfile(Request::post('profile_name', 'profile_address'));
    Redirect::to('login/editUserProfile');
}

型:

    public static function editUserProfile($profile_name, $profile_address)
{
    // write to database, if successful ...
    if (UserModel::saveUserProfile(Session::get('user_id'), $profile_name, $profile_address)) {
        Session::set(array('profile_name', $profile_name, 'profile_address', $profile_address));
        Session::add('feedback_positive', Text::get('FEEDBACK_EMAIL_CHANGE_SUCCESSFUL'));
        return true;
    }
    Session::add('feedback_negative', Text::get('FEEDBACK_UNKNOWN_ERROR'));
    return false;
}   
/**
 * Writes new data to database
 *
 * @param $user_id int user id
 *
 * @return bool
 */
public static function saveUserProfile($user_id, $profile_name, $profile_address)
{
    $database = DatabaseFactory::getFactory()->getConnection();
    $query = $database->prepare("UPDATE users SET profile_name = :profile_name, profile_address = :profile_address WHERE user_id = :user_id LIMIT 1");
    $query->execute(array(':profile_name' => $profile_name, ':profile_address' => $profile_address, ':user_id' => $user_id));
    $count =  $query->rowCount();
    if ($count == 1) {
        return true;
    }
    return false;
}

问题出在数组中,我已经通过执行以下操作解决了这个问题:

public function editUserProfile_action()
{
Auth::checkAuthentication();
UserModel::editUserProfile(Request::post('profile_name'),
Request::post('profile_address'));
Redirect::to('login/editUserProfile');
}

这是通过在模型中执行相同操作来解决的。