SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: UPDATE help

  1. #1
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    USA
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    UPDATE help

    HI GUys

    I am looking for a query to update a current field in a DB.

    Eg:- quantity is currently 5 in the Db I want to update it value to say 5 + 1

    or even 5 + 3.At all times the current value of quantity will be added to.

    THanks
    DIddy

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. The way I would do it is this...
    First get the field you want to update. Lets call it Quantity.
    PHP Code:
    $query "SELECT quantity FROM tbl";
      
    $result mysql_query($query) or die(mysql_error());
      
      while(
    $row mysql_fetch_array($result))
      {
        
    $quantity $row['quantity'];
        
    $query "UPDATE tbl SET quantity = '$quantity' + 3";
        
    $result mysql_query($query) or die(mysql_error());
      } 
    Hope this helps.

    Kevin.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,330
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    kevin, your update statement is inside a loop? that's not right

    it looks like you're updating all rows once for every row in the table

    in other words, if there are 937 rows in the table, you'll run that update statement 937 times, and the update statement will update all rows in the table every time
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    USA
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for replying...I found this solution that worked for me

    UPDATE table set field = field + 1 where.......

    works 100% for me..

    Thanks again
    Diddy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •