doddsey_65 Posted January 16, 2011 Share Posted January 16, 2011 how would i add text onto the end of a varchar field? i have tried using update like so: mysqli_query($link, "UPDATE ".TBL_PREFIX."posts SET post_likes = post_likes + ', $qu' WHERE post_id = '$post_id' ")or die(mysqli_error($link)); but that set the table to 0. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/ Share on other sites More sharing options...
denno020 Posted January 16, 2011 Share Posted January 16, 2011 Query the database, selecting the varchar field that you want to update. Put that into a variable. Append your new data to the end of that variable (using .= ). Then re-write the database with the new variable (which is the same as what was in there before, with the appended data added). Hope that helps Denno Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160210 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 that does help thanks but i have a question. I am adding a like system to my forum similar to facebook where you can like a post then it will display the amount of likes for that post. Presently i have added a post_likes column at the end of the post_table in the database and populated this with usernames. I can get the number of people who like this by counting the array and i can grab the usernames. however i cannot add a date the post was liked and i dont know how i would do that. so i thought of having a seperate table called post_likes where i can save the uid and the pid and the date. but this means a row in the database for every like which could be thousands. which way is better do you think? Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160213 Share on other sites More sharing options...
denno020 Posted January 16, 2011 Share Posted January 16, 2011 You could just have the date appended to the username in the one field.. You'll have to make it a text or even a longtext though. Use a seperator, like |, which you can use with the php function that explodes a string into an array using whatever symbol as the delimiter.. So keep your count in it's own INT type field, and then put the username/date into it's own TEXT or LNGTEXT type field. Doubt this is the beat way, but it should mean that your database table won't get extremely large if there are many people liking things.. Denno Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160216 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 so something in the database like: user1 | date, user2 | date, user3 | date Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160217 Share on other sites More sharing options...
denno020 Posted January 16, 2011 Share Posted January 16, 2011 it'll be like user1|date1|user2|date2|user3|date3 But as the date will always be added directly after the user, you know that they go in pairs. So the 1st and 2nd go together, 3rd and 4th, and even the 19th and 20th etc.. Make sense? Denno Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160218 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 makes sense but how would i pair them? like when i echo them i mean? Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160219 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 another thing is that i am showing who likes the post under the post which means the date will be shown aswell, but i only want the username shown. what about adding another field called like_date after post_likes. data will be added into them at the same time which means the first username in post_likes will match the first date in likes_date Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160222 Share on other sites More sharing options...
denno020 Posted January 16, 2011 Share Posted January 16, 2011 Yeah you would pair them when you build your echo string. If you dont want to show the date when showing who likes the item, then you just iterate and show every 2nd item in the array that is created when the string is exploded . Denno Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160236 Share on other sites More sharing options...
Pikachu2000 Posted January 16, 2011 Share Posted January 16, 2011 Although it sounds like what you're trying to do will result in rather poor database design, the whole operation can be done in one query, without any selecting and manipulating in php. "UPDATE ".TBL_PREFIX."posts SET post_likes = (SELECT CONCAT_WS( ' ', `post_likes`, '$qu') ) WHERE post_id = '$post_id'" Quote Link to comment https://forums.phpfreaks.com/topic/224601-update-varchar-field/#findComment-1160333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.