Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Closed Thread
lil help with adding money
Old 06-23-2010, 02:59 PM lil help with adding money
Novice Talker

Posts: 12
Trades: 0
hello everyone, i want to + and - numbers from the money feild,

so if i click the add $10 and say i have $3 it will == $13
and if i have $200 and click add $10 it will == $210

so with that code

(mysql_query("UPDATE tablename SET field = '$money' WHERE id = '$user_id'"); )

i can only make it set to ten.
xwhitchx is offline
View Public Profile
 
 
Register now for full access!
Old 06-23-2010, 03:02 PM Re: lil help with adding money
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
SET field = field + $money
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 06-24-2010, 04:24 AM Re: lil help with adding money
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
See post below by chris.

What Chris is trying to explain is:

You need to get the exisiting ammount from the table cell:


Code:
mysql_query("SELECT field FROM table WHERE id =  '$user_id'");
Then you put the vars together, I would do this before the query:

PHP Code:
//Best to perform some checks, make sure the value is a number...
if(is_numeric($queryresult['field']){
      
$money $queryresult['field'] + '10';

Then perform the query:

Code:
mysql_query("UPDATE tablename SET field = '$money' WHERE id =  '$user_id'");
__________________

Please login or register to view this content. Registration is FREE

Learn professional skills from professional people, from $6.50 a month.

Last edited by Phunk Rabbit; 06-24-2010 at 06:06 AM..
Phunk Rabbit is offline
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 06-24-2010, 06:00 AM Re: lil help with adding money
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
But you don't NEED to retrieve the value then add it and return it to the database.
That's very inefficient and wasteful of processing time and resources.

USE the database server for what it is good at.

Why make two queries when one will suffice???

Keep data calculations ON THE dB SERVER

PHP Code:
mysql_query("UPDATE tablename SET field = field + $money WHERE id =  '$user_id'"); 
Will add the variable to the field in a single query
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 06-24-2010, 06:04 AM Re: lil help with adding money
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
ahha, my bad chris, didnt know that. Could come in handy

Sorry i got confused with the layout of the query you wrote and (wrongly) assumed it was the logic not the process.

Thanks!
__________________

Please login or register to view this content. Registration is FREE

Learn professional skills from professional people, from $6.50 a month.
Phunk Rabbit is offline
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 06-24-2010, 06:48 AM Re: lil help with adding money
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Yep it's a common thing, especially with programmers who have learnt on MySql alongside and in conjunction with PHP.

It simply isn't understood or appreciated just how powerful database servers and Structured Query Language truly is.
The idea that all DB servers to is store and retrieve data that then has to be manipulated in server code is all too prevalent.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 06-24-2010, 06:50 AM Re: lil help with adding money
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
I am terrible for this (read, lazy), I generally use PHP to do all the manipluation etc and only use SELECT, INSERT, UPDATE and DELETE in thier most basic forms.

I really should read up on the more advanced/less basic querys and methods.
__________________

Please login or register to view this content. Registration is FREE

Learn professional skills from professional people, from $6.50 a month.
Phunk Rabbit is offline
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 06-24-2010, 06:55 AM Re: lil help with adding money
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Aye, you would be surprised at what can be done in a single query, even moreso when you start looking at stored procedures.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 06-24-2010, 06:34 PM Re: lil help with adding money
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by chrishirst View Post
even moreso when you start looking at stored procedures.
True - I'm currently getting into using the PDO Data Abstraction Layer, cool stuff!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
View Public Profile
 
Old 06-25-2010, 04:11 AM Re: lil help with adding money
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
Do either of you have an suggested or reccomended reading about stored procedures?

Ive heard of them however they were being used with .NET and I have no knowledge of .NET
__________________

Please login or register to view this content. Registration is FREE

Learn professional skills from professional people, from $6.50 a month.
Phunk Rabbit is offline
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 06-25-2010, 11:57 AM Re: lil help with adding money
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://msdn.microsoft.com/en-us/libr...SQL.80%29.aspx
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 06-28-2010, 04:31 AM Re: lil help with adding money
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
Thanks Chris
__________________

Please login or register to view this content. Registration is FREE

Learn professional skills from professional people, from $6.50 a month.
Phunk Rabbit is offline
View Public Profile Visit Phunk Rabbit's homepage!
 
Closed Thread     « Reply to lil help with adding money
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33620 seconds with 12 queries