0

I don't know where the problem might be in my code, can someone help me?

In the form field, when I define the 'number' parameter, I want the value in the user_gold column to subtract the gold value that was entered, and the user_bank to add that value

Model

    public static function updateDeposit($deposit)
    {
        // Check input value 
        if ($deposit > self::getUserGame()->user_gold) {

            Session::add('feedback_negative', Text::get('FEEDBACK_BANK_VALUE_FAILED'));
        } elseif ($deposit < 1) {

            Session::add('feedback_negative', Text::get('FEEDBACK_BANK_VALUE_ZERO_OR_NEGATIVE_FAILED'));
        } else {

            $database = DatabaseFactory::getFactory()->getConnection();

            $query = $database->prepare("UPDATE users_game 
                                        SET user_gold = (user_gold - :deposit), user_bank = (user_bank + :deposit)  
                                        WHERE user_id = :user_id");
            $query->execute([
                ':user_gold' => $deposit, 
                ':user_bank' => $deposit,
                ':user_id' => self::getUserGame()->user_id
            ]);

            if ($query->rowCount() == 1) {
                return true;
            }
        }
    }

Controller

    public function deposit()
    {
        BankModel::updateDeposit(Request::post('deposit'));
        Redirect::to('bank');
    }

HTML

        <form method="post" action="<?php echo \App\Core\Config::get('URL'); ?>bank/deposit">
            <label for="deposit">Kwota wpłacana do banku: </label>
            <input type="text" name="deposit" id="deposit" min="0" max="<?= htmlentities($this->user_game->user_gold); ?>" />

            <input type="submit" value='Wpłacam' />
        </form>
  • 1
    You don't have `:deposit` defined in your execute. You also don't have `:user_gold` and `:user_bank` in the SQL. – user3783243 Mar 27 '21 at 11:02
  • I have these columns in my database – GrandeDios Mar 27 '21 at 11:04
  • Your placeholders need to match the bindings in the `execute`. That is how PHP/mysql know which place to put which value. `:user_id` is found and swapped with `self::getUserGame()->user_id`. The rest cause errors because the `:deposit` is not found. The other execute values would throw different errors because the driver also wouldnt know where to put them. – user3783243 Mar 27 '21 at 11:05
  • I don't understand. The deposit value is taken from the form and passed to the model using the post method and is stored in the deposit variable. The deposit value must be specified or an error will pop up. – GrandeDios Mar 27 '21 at 11:18
  • PDO roughly does `str_replace(':anything', $array['anything'], $sql)` for binding swaps. You need to correct the placeholders and bindings. See the dup for longer/more detailed explanation – user3783243 Mar 27 '21 at 11:33
  • think of placeholders in PDO as of variables in PHP. Right now your query is like `$hello = "Greetings"; echo "Greetings $name";` – Your Common Sense Mar 27 '21 at 11:52

0 Answers0