Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    UPDATE and Add at same time

    table abc

    id name qty
    1 banana 2
    2 apple 7


    now i want to update this table and set banana qty to 2(orignianl value)+3 (new value) in one single php mysql statement, how can i do it?


    i do not want to fetch the banna record first , then add the value, then update again, like that is wasting queries and affecting performance
    flying dagger

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code:
    Update abc set qty=qty+1 where id = ...
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    thanks
    flying dagger

  • #4
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    You can also set it up in variables. If you like want a user to submit how many bananas they have.

    PHP Code:
    if (isset($_POST['submit']))
    {
    $bananas $_POST['userbana'];
    $apples $_POST['apples'];

    mysql_query("UPDATE users WHERE bananas=bananas+$bananas, apples=apples+$apples WHERE id='$myid'"); 
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?


  •  

    Posting Permissions

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