Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    86
    Rep Power
    2

    Update table field


    Hi there and Happy New Year... you all good?! I have a small question about a field update...
    How can we do a table field update without replacing the old value but instead adding new next to it ?
    I mean lets have this for example. We have table A that has field b.
    In the b field we have values [1] [2] [3], but when we update the field with value [4] instead of just having value [4] alone could we have values [1] [2] [3] [4] in the field ?
  2. #2
  3. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,504
    Rep Power
    9645
    You can't append to the value - only replace. But you can replace with the final value you want by using the CONCAT function to combine the old value and the thing you're adding in.
    Code:
    UPDATE table SET field = CONCAT(field, new value) WHERE conditions
    Bit curious, though: what are you appending? It's generally a bad idea to have to add values into one column - storing and searching through multiple values like that is quite inefficient...

    Comments on this post

    • pwpx2 agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    86
    Rep Power
    2
    I am trying to update a field from the members table with the data i have from another table. That table contains just some article ids. So in the field from members table i will get [id] [id] [id] after some conditions are met
    Last edited by pwpx2; January 8th, 2018 at 08:02 AM.
  6. #4
  7. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,753
    Rep Power
    4288
    Originally Posted by pwpx2
    So in the field from members table i will get [id] [id] [id]
    eewwww

    you realize how inefficient that is, yes?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    86
    Rep Power
    2
    Perhaps.. but this is what i've got so far.. If it does the job its ok
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2017
    Location
    Worldwide
    Posts
    39
    Rep Power
    44
    If it does the job its ok
    If you have any plans to be a REAL programmer, toss that idea right now.

    Comments on this post

    • r937 agrees : harsh but true
  12. #7
  13. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,504
    Rep Power
    9645
    Originally Posted by pwpx2
    Perhaps.. but this is what i've got so far.. If it does the job its ok
    Today, maybe. It will probably work fine tomorrow too, and next week, and next month. But eventually it will break down and you will need to fix it. Fix it now before it's too late and you start losing data and your site starts suffering.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    86
    Rep Power
    2
    I've tried the concat code and it doesnt do what i desired. In fact nothing happens...
    Code:
    mysqli_query($GLOBALS["___mysqli_ston"], "UPDATE members SET access_special = 'yes', article_special = CONCAT(article_special, [" . sqlesc($id). "]) WHERE id = " . sqlesc($arr["uid"]) . "") or sqlerr(__FILE__, __LINE__);
  16. #9
  17. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,753
    Rep Power
    4288
    Originally Posted by pwpx2
    I've tried the concat code and it doesnt do what i desired. In fact nothing happens...
    i can help you pinpoint whether it's an sql problem or a php problem
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    86
    Rep Power
    2
    why would be php problem ?
    Last edited by pwpx2; January 10th, 2018 at 04:28 PM.
  20. #11
  21. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,753
    Rep Power
    4288
    why would it be an sql problem?

    come on, man, test it in mysql, outside of php
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    86
    Rep Power
    2
    It's no php error since i dont' get anything related in the logs.
  24. #13
  25. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,753
    Rep Power
    4288
    test. it. in. mysql.

    Comments on this post

    • pwpx2 agrees
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    86
    Rep Power
    2
    yes. i did it in mysql and the error was some the brackets inside the concat function for the $id
    thank you
    edit: one small issue is that i am using UPDATE IGNORE and it still gives duplicate values in the field
    Last edited by pwpx2; January 12th, 2018 at 02:06 AM.
  28. #15
  29. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,753
    Rep Power
    4288
    Originally Posted by pwpx2
    edit: one small issue is that i am using UPDATE IGNORE and it still gives duplicate values in the field
    could you show a before/after of the row which includes the dupes
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo