Help a noob turn 2 update's into 1

Discussion in 'Databases' started by Dirty-Rockstar, Oct 27, 2008.

  1. #1
    Ive tried to figure this one out. Selects I can use join. I have that figured out. But update? No clue. I tried using "IN" but im lost. if someone could push me in the right directrion it would be awesometastic. Trying to clear up buggy noob code ;)

    
    /*
    *Subtracting Money from user A, Adding to user B
    *Simple money transfer script (fantasy money of course :P)
    *can this be done in 1 query?
    */
    
    
    mysql_query("update users set money=money-'$amount' where id='$users[id]'") or die (mysql_error());
    mysql_query("update users set money=money+'$amount' where id='$recieve_id[id]'") or die (mysql_error());
    
    PHP:
     
    Dirty-Rockstar, Oct 27, 2008 IP
  2. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Read some more about transactions with those you can make the assumption that all queries or none of the queries get COMMITED to the database.
     
    chisara, Oct 27, 2008 IP
  3. ranabra

    ranabra Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    To futher streess what "chisara" has pointed out. if you are really using this as displayed in your code sample for money, even "fantasy money" you must use a transaction with a rollback of it fails or if an error occured in the middle of it all
     
    ranabra, Oct 27, 2008 IP
  4. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I told you how you can fix it, but ranabra's advice is a bit more important, he tells you that you must use transactions/rollbacks.
    You must use transactions when doing something like this.
     
    chisara, Oct 29, 2008 IP
  5. greny

    greny Peon

    Messages:
    162
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You still can do JOIN's in UPDATE. Here's a example based on your UPDATE query:

    
    UPDATE
    users as a1,
    users as a2
    
    SET
    a1.money = a1.money-$amount,
    a2.money = a2.money+$amount
    
    WHERE
    a1.id=$users[id] and
    a2.id=$recieve_id[id]
    
    Code (markup):
    PS. Don't forget to check if $users[id] even has the amount to send. :) You don't want his balance to go negative. :D
     
    greny, Nov 6, 2008 IP
  6. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That looks charming greny, I even suspect that it will work, learn something new every day.
    But I have my doubts when you include the has the amount to send in the where clause.
    I suspect concurrent threads might produce a negative result but I suspect that 99,99% of the SQL users wouldn't be able to answer the safety of that construction with 100% certainty.
    With fully serialised transactions you could be sure of the consistency, but if a master of SQL could analyze this update join construction and answer I would be very gratefull to learn if this construction is "safe".
     
    chisara, Nov 6, 2008 IP