Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"UPDATE WHERE NOT LIKE? "

I am trying to append strings to a field if they do not already exist with:

mysql_query("UPDATE gals4 SET tags = CONCAT(tags, '$instag') WHERE id = '$instagnum' AND tags NOT LIKE '$instag'");

This just appends to 'tags' regardless of weather it exists in the field or not. What am i doing wrong here?

like image 855
user1717656 Avatar asked Sep 05 '25 03:09

user1717656


1 Answers

To answer your immediate question, you must put the character % at the beginning and end of the match string:

 "AND tags NOT LIKE '%$instag%'"

However, you should be aware that this is a terrible way to store data in an SQL database. There are at least three problems:

  1. If you have tags that embed other tags ("cat" and "scat" for instance) you will find the wrong records unless you write very complicated comma-based searches.
  2. These searches can never be indexed and will therefore become very slow as the number of records grows.
  3. You cannot verify the tags used against a list of allowed tags, guarantee that only allowed tags are in the database, or easily present a list of existing or allowed tags.

The correct solution is to add at least one table to your database, called something like gals_tags, with columns galid and tag. Insert one record per tag into this table. If a gal has more than one tag, add one record for each tag.

like image 181
Larry Lustig Avatar answered Sep 07 '25 16:09

Larry Lustig