Jump to content

update varchar field


doddsey_65

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.