0

I want to make it so the user can essentially add $10,000 to their current balance.

To do this, I made a user with id 26 that has $10,000. I want to add user26's funds to the current user's funds to increase the total by $10,000. When you press a button that says 'add money,' it'll give you $10,000 and redirect you to your portfolio. However, it gives me this error: Notice: Array to string conversion in /home/ubuntu/workspace/pset7/public/money.php on line 17 (the one that says "add the number to the current...").

Fatal error: Unknown column 'Array' in 'field list' in /home/ubuntu/workspace/pset7/vendor/library50-php-5/CS50/CS50.php on line 352

I thought about just making

            $total = $currentbalance + 10000;

But it tells me Fatal error: Unsupported operand types in /home/ubuntu/workspace/pset7/public/money.php on line 14

How can I make my 'add fake user's money to real user's money' plan work? (Or, failing that, how can I add directly to the amount of money a user has?)

<?php

// configuration
require("../includes/config.php");

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
        //how much money does user have right now
        $currentbalanace = CS50::query("SELECT cash FROM users WHERE id = ?",$_SESSION["id"]);

        //get the amount of money located at user #26, which is $10000
        $tenthou = CS50::query("SELECT cash FROM users WHERE id = 26");

        $total = $currentbalance + $tenthou;

        //add the number to the current balance and update it in the database
        CS50::query("UPDATE users SET cash = $total WHERE id = ?", $_SESSION["id"]);

        //redirect to portfolio
         redirect("./");

}

else {
render("money_form.php", ["title" => "Money"]);
}

?>
1
  • I don't know what platform you are taking the course on, but this last year's version of the pset. The current version uses python and sqlite. – DinoCoderSaurus Mar 29 '17 at 10:52
0

The problem arises because, remember, the query returns an array, whether you select one row or a million rows.

From the spec:

If query is passed a SELECT statement, it will return an array with 0 or more rows.

You can do the update in one easy sql, no SELECTing necessary:

UPDATE user set cash = cash + 10000 where id = .....

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .