Help with reducing the value and updating

Can you tell me where I am incorrect in this code.
I’m trying to deduct -.5 when the condition is met, and then update the ‘wallet’ in the db.

$sell_video = $video_cost;
$uploader_account = $db->where('id', $video->user_id);

if ($sell_video == 0 && $uploader_account >= 1) {
$upwallet = (float)$uploader_account -0.5;
}

// new balance?
$uploader_account = $upwallet;

// update the record?
$db->where('id', $video->user_id);
$upwallet = $db->update('wallet' => number_format($uploader_account, 1, '.', '');

currently, there is no change to the db value upon transaction.
any guidance/suggestions - appreciated

Looking at $db->where('id', $video->user_id what are you expecting it to be? Is that you see when echoing it out?

Over the years, I have read and occasionally helped with threads for first the ‘php motion’ script and now this ‘PT’ script you are trying to use. You are the only person here that knows and has access to the meaning of the data that these methods return and require as parameters. No one here can help just based on the non-working snippets of code you are posting. You must provide information/links to the documentation for these methods.

About a year ago, you posted a similar thread - Help with getting correct amount into table column where you are performing a calculation and updating a column with the resulting value. Unless you changed what these methods do, the ->where() method call returns an object and uses additional syntax to get only a singe row of data, and the syntax you are using for the ->update() method call in this thread is largely different.

So, sorry, but small snippets of non-working code that apparently don’t even use the correct syntax for the script you are using are not helpful. Does this PT script have its own support forum you can post your questions on, where someone already familiar and up to speed with its inner workings can actually help?

1 Like

Well mab has clearly given up, but even without a PT support forum, we can at least point out basic PHP problems.

I’m going to guess thats because PHP is having a shitfit with your code.

Consider: What happens if $sell_video is 1? Specifically, what happens when you get to the line you’ve marked “new balance?”

1 Like

I may be pretty off as this all foreign to me… Also I may use different variable names as I have seen variable values being passed to another variable for seemingly no reason and I just want to use something I can keep straight. You want to subtract from the users wallet a set amount based on how much they have and other factors so I will define that variable as $userswallet. Correct me if I am wrong but I believe you can get the users wallet amount like so.

$userswallet = $user->wallet;

Then applying this to your IF condition we have

if ($sell_video == 0 && $userswallet >= 1) {
	$upwallet = (float)$userswallet -0.5;
}

Now I think everything is defined in kind of a reverse order so I will start with the field->values and define it as $data.

$data = array (
	'wallet' => $upwallet
);

Then we start the query building starting with the WHERE condition. I believe you have that right assuming we want the user_id from the video record.

$db->where ('id', $video->user_id);

Then on the UPDATE line, I think you were missing the table name, so I put it together like this.

$db->update ('users', $data);

Hey, not sure if it is right but I gave it a go.

$userswallet = $user->wallet; 

if ($sell_video == 0 && $userswallet >= 1) {
	$upwallet = (float)$userswallet -0.5;
}

$data = array (
	'wallet' => $upwallet
);
$db->where ('id', $video->user_id);
$db->update ('users', $data);

Note: This was being composed before m_hutley posted but he beat me to the submit button. I will post it anyway.

1 Like

You’re much more informative than mine Drummin, you go with it.

Though on Mab’s point… the thread linked in that post has OP’s code example for retrieving data as having another function chained on to the end of the where…

So… are we sure we’ve actually got the functions correct, OP?

1 Like

Thanks for all the replies.
After testing your suggestion, no, successful deduction yet…

And yes, I’m trying chain this function onto the end of the buy-video.php file.

Earlier in that file it shows this:

.

....... etc.

// add data to table
			$insert_buy = $db->insert('videos_transactions', [
			'user_id' => $video->user_id,//seller
			'paid_id' => $user_id,//buyer
			'video_id' => $video_id,
			'amount' => (string)$video_cost_new,
			'admin_com' => $site_add_amount,
			'time' => $time_start,
			'type' => buy,
			'session_key' => $_SESSION['session_key']
			 ]);


			// count successful inserted records
			if ($insert_buy) {
				$inserted_records++;
			}

			//update the 'balance' of the user who uploaded the video
			// get the user's record
			$up_user_start = $db->where('id', $video->user_id)->getOne(T_USERS);

			// add to the balance
			$uploader_account = $up_user_start->balance+$up_amount;

			// update the record
			$db->where('id', $video->user_id);
			$update_balance = $db->update(T_USERS, [
			'balance' => number_format($uploader_account, 1, '.', ''),
			]);

		// reflect the user 50% of the up_amount to show actual money amount
		 $money_amount = $up_amount *0.50;

		//update the 'money' of the user who uploaded the video
		// get the user's record
		$money_user_start = $db->where('id', $video->user_id)->getOne(T_USERS);

		// add to the money
		$uploader_account = $money_user_start->money+$money_amount;

		// update the record
		$db->where('id', $video->user_id);
		$update_money = $db->update(T_USERS, [
		'money' => number_format($uploader_account, 2),
		]);

In this code there is a buyer/user and a seller/uploader.
In the db ‘users’ table there is a ‘balance’ column and a ‘wallet’ column.

I’m trying to add money to the user/uploader (not user/buyer) wallet (not balance). So, based on that, would this still be correct, as show in #5:

$userswallet = $user->wallet;

#5 may be correct possibly up until the UPDATE.

Based on the additional code I’ve provided, can you make another suggestion?
I look forward to your comments. Thanks again

No. Nor would SUBTRACTING from the user balance $uploader_account -0.5; if you are wishing to add to the sellers balance. You have $upwallet and said

I’m trying to deduct -.5 when the condition is met, and then update the ‘wallet’ in the db.

So my post was in response to this.

Now it has been noted by m_hutlet your other thread where you UPDATE the user account and in #10 you say it is working. It would be very helpful to everyone if you posted examples of “working” code, so people don’t have guess the correct formatting for update queries.

I would probably not use number_format() IN THE QUERY and I can’t think of a time I needed to format a number to add it to the database, but again I would probably have the field type set as decimal(12,2) for these values.

So back to your updates.
YOU know what you want to happen and YOU should have a much clearer vision of what each snip of code does. It looks like you are getting the SELLERS array of data defined as $uploader then adding $up_amount to $uploader->balance with the result defined as $uploader_account.

//SELLER: update the 'balance' of the user who uploaded the video
// get the user's record
$uploader = $db->where('id', $video->user_id)->getOne(T_USERS);

// add to the balance
$uploader_account = $uploader->balance+$up_amount;

The update might go

$db->where('id', $video->user_id);
$update_balance = $db->update(T_USERS, [ 'balance' => $uploader_account ]);

For the buyer you go about getting their wallet amount in the same way and subtract the fee amount -.5 (or amounts you need to) and make the update using appropriate fields and id’s.

I would NOT be doing an IF condition for wallet balance during this “UPDATE” part of the code i.e. if ($sell_video == 0 && $userswallet >= 1) { as this is the kind of priority condition that determines IF a sale is going through or not. which would wrap around all sale and update queries.

You keep asking for suggestions but not really saying what you’ve tried and what is not working about the code. As I said, It would be very helpful to show a working UPDATE query and one you are currently working on so they could be compared.

Or is it SELLERS wallet? If so, change queries as YOU need and post back.

Thank you again for your replies. Sorry for any confusion. I mis-typed when I said: I’m trying to add money to the user/uploader (not user/buyer) wallet (not balance).", I meant I’m trying to reduce the amount in the user/uploader wallet (not user/uploaders balance). What I’m trying to succeed with here should have nothing to do with “balance”.

Regarding providing examples of updating code, I thought what I provided in #7 was that.
With regard to ‘what I’ve tried’ it is many, many things, would be too much to post.

But I’ve just tried this with no deduction success:

$sell_video = $video_cost;

if ($sell_video == 0 && $uploader_account >= 1) {
$upwallet = (float)$uploader_account -0.5;
}

$uploader_account = $uploader->$upwallet;

$db->where('id', $video->user_id);
$update_wallet = $db->update(T_USERS, [ 'wallet' => $uploader_account ]);

Where in your site/code do you have a successful update query? Please post this query as an example.

Why are just passing a variable value to another variable?
$upwallet should be defined regardless of entered amount as you are using this variable to adjust the “wallet” amount.

$upwallet = ($sell_video == 0 && $uploader_account >= 1 ? (float)$uploader_account -0.5 : 0);

So $upwallet will be -0.5 or 0 based on the conditions.

Now you get the sellers array of data

//SELLER: update the 'wallet' of the user who uploaded the video
// get the user's record
$uploader = $db->where('id', $video->user_id)->getOne(T_USERS);

As an example $uploader->wallet would call the wallet amount from the array.
Then you add $up_amount to the wallet amount.

// add `$upwallet` to the wallet
$uploader_account = $uploader->wallet+$upwallet;

And I’ll have to stick to the same update query until I see another example.

$db->where('id', $video->user_id);
$update_balance = $db->update(T_USERS, [ 'wallet' => $uploader_account ]);

For those that don’t know I am basing the query style on this select example where $db->join and $db->where are added to $db->get.

$db->join("users u", "p.tenantID=u.tenantID", "LEFT");
$db->where("u.id", 6);
$products = $db->get ("products p", null, "u.name, p.productName");
print_r ($products);
// Gives: SELECT u.name, p.productName FROM products p LEFT JOIN users u ON p.tenantID=u.tenantID WHERE u.id = 6

Until I see otherwise I will use this.

Thanks again.
I believe you’re saying to try this:

$upwallet = ($sell_video == 0 && $uploader_account >= 1 ? (float)$uploader_account -0.5 : 0);

$uploader = $db->where('id', $video->user_id)->getOne(T_USERS);

$uploader_account = $uploader->wallet+$upwallet;

$db->where('id', $video->user_id);

$update_balance = $db->update(T_USERS, [ 'wallet' => $uploader_account ]);

which, when tested, makes no deduction when the sell price is 0, but reduces the wallet amount (of 5, in the test) to zero when the sell price was 1, for example.

Also, regarding a query, this from another file in the script in regard to another function, but may help as a guide possibly?

$update = array('wallet' => $db->dec($total));

$db->rawQuery("UPDATE ".T_USERS." SET `balance` = `balance`+ '".$balance."' , `verified` = 1 WHERE `id` = '".$video->user_id."'");

I look forward to any replies.

When you display the values of each of the variables in your first piece of code, do they contain what you expect them to? How far through that code does it get before it doesn’t do what it should?

If you just want to just subtract the -0.5 from wallet then it wouldn’t be

$uploader_account -0.5

would it?
You would just subtract the -0.5.

Also if you can do a raw query THEN there is no need to make queries to get the wallet amount and calculate the new amount. A standard query style would work. This is all you need.

$upwallet = ($sell_video == 0 && $uploader_account >= 1 ? .5 : 0);
$db->rawQuery("UPDATE ".T_USERS." SET `wallet` = `wallet`- '".$upwallet."' WHERE `id` = '".$video->user_id."'");

Just noting that you want to ADD to the seller’s wallet amount (right) so you would use + in the query.

$db->rawQuery("UPDATE ".T_USERS." SET `wallet` = `wallet`+ '".$upwallet."' WHERE `id` = '".$video->user_id."'");

you also probably shouldnt wrap a number in quotes, but…

1 Like

Yes, good spot. I was originally thinking that maybe the OP stores numbers as formatted strings for some reason (which might tie in with some earlier comments) but of course that wouldn’t work when using the number inside quotes within a query string.

As you say, there is a big difference between

set `wallet` = `wallet` + '1.50' where ...

and

set `wallet` = `wallet` + 1.50 where

But surely the database class that the OP is using, whatever that is, would throw an error if they try to add a string to a numeric column.

Most DB engines should coerce items on both sides of an arithmetic operator to a Number type and perform the arithmetic operation if possible. Most DB engines should have a specific string concatenation function (CONCAT in most engines). But in general… eh.
Also, insert standard ‘dont use a raw string query if a parameterized one is available’ caveat. (because then your database connection library handles the quotes for you!)

Correct. I just copied his “working query” from #12.
And from the other mentioned post.