PHP MySql Update Where problem

Discussion in 'PHP' started by gilgil2, May 2, 2012.

  1. #1
    Hi I can't work out why this is not working. It works in a separate table or if I don't do Update Where, just Insert into, so that it works on a new row but does will not update the row I want.

    Any help would be very appreicated.

    
    function updatePayments($data){        
    global $link;    
    if(is_array($data)){
    $sql = mysql_query("UPDATE `gigs` SET (txnid, payment_amount, payment_status, itemid, createdtime) 
    VALUES ('".$data['txn_id']."' , '".$data['payment_amount']."' , '".$data['payment_status']."' , '".$data['item_number']."' , '".date("Y-m-d H:i:s")."')
    WHERE`gigname` ='".$data['item_number']."'", $link);    
    return mysql_insert_id($link);
        }
    }
    PHP:
     
    gilgil2, May 2, 2012 IP
  2. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #2
    You are using wrong syntax.
    Here's an example of correct updating syntax.
    UPDATE tablename SET something='something' WHERE something = 'something'
    Code (markup):
     
    Arttu, May 2, 2012 IP
  3. gilgil2

    gilgil2 Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks for your help Arttu, I have changed that but it still doesn't working.

    I now have this:

    
    function updatePayments($data){    
        global $link;
        if(is_array($data)){                
            $sql = mysql_query("UPDATE `gigs` SET txnid = '".$data['txn_id']."' ,
    payment_amount = '".$data['payment_amount']."' ,
    payment_status = '".$data['payment_status']."' ,
    itemid = '".$data['item_number']."' ,
    createdtimed = '".date("Y-m-d H:i:s")."'
        WHERE gigname='".$data['item_number']."' ", $link);
        return mysql_insert_id($link);
        }
    }
    ?>
    
    Code (markup):
    Is there anything else I am doing wrong?
     
    gilgil2, May 2, 2012 IP
  4. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #4
    Remove quotes around the table name.
     
    Arttu, May 2, 2012 IP
  5. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #5
    error is at the and of the query


    $sql = mysql_query("UPDATE `gigs` SET txnid = '".$data['txn_id']."' ,payment_amount = '".$data['payment_amount']."' ,payment_status = '".$data['payment_status']."' ,itemid = '".$data['item_number']."' ,createdtimed = '".date("Y-m-d H:i:s")."' WHERE gigname='".$data['item_number']."' ", $link);
     
    webshore88, May 2, 2012 IP
  6. gilgil2

    gilgil2 Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    Thanks, which bit do I need to change at the end?

    Arttu, thank you, I tried that though and it still is not working... I am completely lost!
     
    gilgil2, May 2, 2012 IP
  7. gilgil2

    gilgil2 Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Thanks for the help but I have decided to do it a different way instead, using two tables.
     
    gilgil2, May 2, 2012 IP
  8. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #8
    That's not an error, that just tells PHP to use a specific connection.


    I tested the query and it works, which means that the error occurs because of something that those variables contains. Also, I just realized that those were backtacks around the table name not single quotes, you can put them back if you want(that's not what causes it to not work).
     
    Arttu, May 2, 2012 IP
  9. webxtech

    webxtech Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i asking You ONe Question What is Coding and why are you use in language html,Php,xhtml and etc .So please all developer answer this Question .
     
    webxtech, May 3, 2012 IP
  10. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #10
    what is in $link?
     
    webshore88, May 4, 2012 IP
  11. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #11
    a MySQL link identifier

    eg.
    
    $link1 = mysql_connect('server', 'mysql_user', 'mysql_password');
    $link2 = mysql_connect('server2', 'mysql_user', 'mysql_password');
    
    mysql_query('SELECT * WHERE 1=1', $link1);
    
    PHP:
    link1 will be used.
     
    Arttu, May 5, 2012 IP
  12. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #12
    all right :)
     
    webshore88, May 14, 2012 IP
  13. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #13
    Be careful of SQL Injection if the values are taken directly from user input.
     
    NetStar, May 20, 2012 IP
  14. manhtv157

    manhtv157 Peon

    Messages:
    24
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    3
    #14
    I think you wrong at mysql syntax . let use basic syntax
    UPDATE tablename SET field1='value1',field2='value2',....,fieldn='valuen' WHERE field= 'condition'
    Other .+ your field name should not
    duplicate with MYSQL constant and MYSQL keyword .such as : name, keyword ,desc
    + your string which you update must not
    contains sql injection . such as : ' , / ,

    . Let's check. Goodluck
     
    manhtv157, May 20, 2012 IP