Solved

PHP MySQL update supquery

Posted on 2011-02-24
7
393 Views
Last Modified: 2012-05-11
I'm sure this is a pretty simple question, but I can't think of a solution at the moment.  I'm trying to update a decimal value, based on the current value of the field.

I want to reduce the amount in the field by the amount in a variable... something like this...

"currentrequest" is the current field...  So, basically I want to update current request to be it's current value minus 100.00.  

I've tried using a select subquery in there, and it tells me that I can't select the same table...  
The error I receive is: "You can't specify target table 'budget' for update in FROM clause".

Thanks for any shoves in the right direction!


$reqAmt=100.00;

$removeFromBudget=@mysql_query("
   UPDATE budget 
   SET currentrequest='currentrequest-".$reqAmt."' 
   WHERE acct='".$acctNum."' LIMIT 1");

Open in new window

0
Comment
Question by:cbastian-hill
  • 4
  • 3
7 Comments
 

Accepted Solution

by:
cbastian-hill earned 0 total points
ID: 34972441
Just found the answer...   Posting in case anyone else needs it...


$removeFromBudget=@mysql_query("
   UPDATE budget 
   SET currentrequest=((SELECT currentrequest)-".$reqAmt.")
   WHERE acct='".$acctNum."' LIMIT 1");

Open in new window

0
 
LVL 17

Expert Comment

by:nanharbison
ID: 34972454
Can you give us the whole query? I don't see a subquery here.
0
 

Author Comment

by:cbastian-hill
ID: 34972473
That is the whole query...  The sub query is in the SET currentrequest..... line...

Are you not seeing that line?  Or am I describing it incorrectly?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Expert Comment

by:nanharbison
ID: 34972943
That's not a subquery, but glad you found the answer!
0
 

Author Comment

by:cbastian-hill
ID: 34972973
Interesting...  I was always taught that anything like that inside another query would be considered a subquery.  Would you know what it should be called, then?
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 34973097
It just doesn't look like a subquery because UPDATE a table, set something equal to a value is a regular update query, right? But you do have to select a value first before you can change it by doing math on that value...hmm, not sure.Maybe you are right?
0
 

Author Comment

by:cbastian-hill
ID: 34973139
Hmmm....  That is what I was thinking...  The SELECT inside the UPDATE would be a subquery...  I'll have to go back to the books later, when I have some time, and see if I can find an exact definition of what makes a subquery...   Thanks for the info, either way!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question