我有这个操作,我希望将 $newtkamount
保存到数据库中,我尝试使用与我的注册页面相同的代码(显然指向不同的表),但我无法解决这个问题。需要根据当前用户的user 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;
}
}
最佳答案
始终在保存数据后渲染,并且不要在 Controller 中使用 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));
}
关于php - 将数据保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25426877/