php - 将数据保存到数据库

标签 php mysql yii

我有这个操作,我希望将 $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/

相关文章:

php - 如何将 SELECT 查询的结果分配给变量?

php - 如何在MySQL中连接分层表(表连接其他表,连接其他表,连接其他表)?

php - Yii 按关系列 HAS_MANY 排序

php - Yii 查询生成器结果(带连接和子查询的纯 SQL)

php - TbNavbar 下拉项内的表单

php - Elephant.io客户端无法正常工作

php - 使用 PHP 检查并删除多维数组中的数组值

MySQL根据特定搜索字符串导出行内容

mysql - 迭代替换子字符串的 SQL 函数

MySQL服务器5.6 : Why my time zone is greater than UTC 6 minutes