Help With Inserting Data

Discussion in 'PHP' started by Tintumol, Dec 14, 2010.

  1. #1
    Well I made a tag system and it all works. Well when someone posts a blog they enter tags and each tag needs to go in as 1 tag each field. So if someone types "This is my tags"

    And in the database it needs to be like this (And sorry if the example isnt good)
    ID|Tag
    -------
    1 |This
    2 |is
    3 |my
    4 |tags

    And I know before inserting to use array_unique to remove duplicates since I dont need duplicates.

    But I dont know how to get each tag in its own special field in the database
     
    Tintumol, Dec 14, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    
    $array = explode(" ",$tags);
    foreach($array as $key => $value)
    {
    mysql_query("INSERT INTO tags_table VALUES (NULL,'".$value."') WHERE '".$value."' NOT IN (SELECT Tag FROM tags_table WHERE Tag = '".$value."')");
    }
    
    PHP:
     
    tvoodoo, Dec 14, 2010 IP
  3. tnd8

    tnd8 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can try to set "id" and "tag" fields are "key", so you CAN NOT insert duplicate data to that table :)
     
    tnd8, Dec 14, 2010 IP
  4. ankit_frenz

    ankit_frenz Active Member

    Messages:
    1,111
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    63
    #4
    you should use explode first with spaces as suggested by tvoodoo..however better approach would be ask your users to enter tag line by line and explode using php_EOL
     
    ankit_frenz, Dec 14, 2010 IP