Jump to content

PHP/MYSQL concat update if unuqie


Cardale

Recommended Posts

Well if you wanted to know how, wouldn't a better question have been, "How do you preform an update statement with concat if unique?" instead of, "Can you"? Note this sticky.

 

Anyhow, in order to help you why not give us something to work with, as I do not fully understand what you want done, perhaps some sample code you have tried or some sample data of what you want to happen to it. That will help get your topic solved a lot quicker.

Link to comment
Share on other sites

? something to work with?  Ok...I guess didn't think it was that complicated maybe I am wrong. 

UPDATE users SET iplog = CONCAT(iplog, '$ip')  WHERE id='$id'

 

I need to update a users ip in my concat list and only update it if the ip isn't already in the list.

Link to comment
Share on other sites

How it was explained made little sense as I did not know you were working with IP's. Now the question comes into play, why are you adding multiple ips in one column? Why not create an iplog table and add them there, this would normalize the database and probably be preferred?

 

To answer your question, however, here is the code:

 

UPDATE users SET iplog = CONCAT(iplog, '$ip') WHERE id = '$id' AND iplog NOT LIKE '%$ip%' 

 

Should do the trick.

Link to comment
Share on other sites

If I wanted to look and see what ips have been used by a user a ip table wouldn't be as efficient I believe.  Also I haven't done this.  With this method I know exactly what ips where use by said user.  Now getting that information out in a reliable way will also be a new adventure for me.

Link to comment
Share on other sites

Well it would actually be really easy with an ip table. You setup the id column as auto_increment, a userid column and an ip column.

 

Make the id primary key then anytime you wanna see the ip's a user has used you just do a join query. It is actually how most databases are done and is required for a 3rd normalized form database structure.

 

But whichever way works best for you.

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.