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

Reply
mysql update with condition
Old 03-22-2011, 11:43 AM mysql update with condition
Skilled Talker

Posts: 83
Trades: 0
Hello,

I am using the following query:

mysql_query("UPDATE products SET products_quantity = products_quantity - ".$amount_bought.", products_status = IF (products_quantity - ".$amount_bought." < 1, 0, 1) WHERE products_model='".$model."'");

The problem is that products_status is set to 0, even if the remaining products_quantity is 1.


What did i do wrong?

thanks!

Matt
killerwhale65 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-22-2011, 12:31 PM Re: mysql update with condition
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
No idea, do we get to see the query rather than PHP code?
__________________
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 online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-22-2011, 12:33 PM Re: mysql update with condition
Skilled Talker

Posts: 83
Trades: 0
UPDATE products SET products_quantity = products_quantity - 1, products_status = IF (products_quantity - 1 < 1, 0, 1) WHERE products_model='ADO_AP Churchill'
killerwhale65 is offline
Reply With Quote
View Public Profile
 
Old 03-22-2011, 12:45 PM Re: mysql update with condition
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
How can a "less than one" match be true when the result is 1??
__________________
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 online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-22-2011, 01:27 PM Re: mysql update with condition
Skilled Talker

Posts: 83
Trades: 0
no idea what you mean, but if stock drops to 0 or less, status must be disabled.
killerwhale65 is offline
Reply With Quote
View Public Profile
 
Old 03-22-2011, 01:48 PM Re: mysql update with condition
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
then your query should be IF(criteria <= 0 ...) [less than OR equal to] ZERO

From your post ->
Quote:
The problem is that products_status is set to 0, even if the remaining products_quantity is 1.
Your actual query was checking for the result being LESS THAN ONE which a result of 1 cannot be.
__________________
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 online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-22-2011, 02:01 PM Re: mysql update with condition
Skilled Talker

Posts: 83
Trades: 0
ok thanks will try that!
killerwhale65 is offline
Reply With Quote
View Public Profile
 
Old 03-23-2011, 10:33 AM Re: mysql update with condition
Skilled Talker

Posts: 83
Trades: 0
i now have this query:

UPDATE products SET products_quantity = products_quantity - 1, products_status = IF (products_quantity - 1 <= 0, 0, 1) WHERE products_model='AP Churchill'

But products_status is still set to 0 when the stock drops from 2 to 1.
killerwhale65 is offline
Reply With Quote
View Public Profile
 
Old 03-23-2011, 11:30 AM Re: mysql update with condition
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Your query has already deducted 1 from quantity BEFORE the the IF is evaluated.
__________________
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 online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-23-2011, 11:34 AM Re: mysql update with condition
Skilled Talker

Posts: 83
Trades: 0
oh, thats how it works, ok didn't know that. I will try reversing both.
killerwhale65 is offline
Reply With Quote
View Public Profile
 
Old 03-23-2011, 11:45 AM Re: mysql update with condition
Skilled Talker

Posts: 83
Trades: 0
works now, thanks!
killerwhale65 is offline
Reply With Quote
View Public Profile
 
Old 07-28-2011, 05:54 AM Re: mysql update with condition
Skilled Talker

Posts: 83
Trades: 0
Hello,

Sorry to start this old thread again, but what if i want the update condition do nothing if the condition is false?

Right now
products_status = IF (products_quantity - 1 <= 0, 0, 1)

updates products_status to 0 if condition true, and to 1 if false.

But if false i want nothing to happen, how can i do that?

Thanks!

Matt
killerwhale65 is offline
Reply With Quote
View Public Profile
 
Old 07-28-2011, 08:52 AM Re: mysql update with condition
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
have nothing at all for the "false" codition
__________________
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 online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-28-2011, 09:14 AM Re: mysql update with condition
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
It seems to me that your information is reduntant. If you already have the quantity, why do you need another field to say if there are more items or not? The quantity field would be enough. Every time you check if the status field is 0 or 1, you could instead check if the quantity is 0 or >0, thereby eliminating the status field completely.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 07-28-2011, 07:30 PM Re: mysql update with condition
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
Quote:
Originally Posted by lizciz View Post
It seems to me that your information is reduntant. If you already have the quantity, why do you need another field to say if there are more items or not? The quantity field would be enough. Every time you check if the status field is 0 or 1, you could instead check if the quantity is 0 or >0, thereby eliminating the status field completely.
Status might be used for other things - eg adding a new range in advance of launch with a status indicating "don't publish in catalogue".
__________________

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


*** New:
Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to mysql update with condition
 

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 4.70935 seconds with 12 queries