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.

? 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.

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.