将数据保存到数据库

时间:2014-08-21 12:54:19

标签: php mysql yii

我有这个动作,我想保存,$newtkamount到数据库,我尝试使用我用于我的注册页面的相同代码(显然指向不同的表)但我无法得到什么都可以解决这个问题。它需要根据当前用户用户ID

重写已存在的值
public function actionBuy($qty=0) {
    $_id = Yii::app()->user->getId();
    $model = Tokens::model()->findByAttributes(array('UserID' => $_id));
    if ($model === null)
        throw new CHttpException(404, "Keep calm! If you havent bought tokens before this is normal");


    $this->render('buy', array(
        'model' => $model,));

    if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'){
        $_qty = $_POST['qty'];
        $newtkamount = ($model->TokenAmount + $_qty);

        echo $newtkamount . $model->TokenAmount;
    }
}

1 个答案:

答案 0 :(得分:1)

保存数据后始终渲染,不要在控制器中使用echo

public function actionBuy($qty=0) {
    $_id = Yii::app()->user->getId();
    $model = Tokens::model()->findByAttributes(array('UserID' => $_id));
    if ($model === null)
        throw new CHttpException(404, "Keep calm! If you havent bought tokens before this is normal");

    if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'){
        $_qty = $_POST['qty'];
        $model->TokenAmount = ($model->TokenAmount + $_qty);
        $model->save(false);
    }

    $this->render('buy', array(
        'model' => $model));
}