SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jul 2003
    Location
    Toronto, Ontario
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Insert Ignore Into?

    Hey guys.

    I have tried to omit inserting duplicate entries with a few functions but can't seem to get any of them to work.

    The only one that came close was IGNORE, however it will insert the same rows over and over again everytime the command is run.

    So after I run it once, the directors table has 12,000 entries, the second time it has 24,000 and so on every time the query is run. I just want it to ignore the entries that are already in the table.

    Any ideas how I can prevent these entries from being inserted multiple times?

    Code:
    while($row = mysql_fetch_assoc($result)){
    
    	mysql_query("INSERT IGNORE INTO directors 
    	(name) VALUES('" . $row['Director_01'] . "')");
    			
    	 echo '<tr>';
    		echo '<td><p class="link"><a href="#">',$row['Director_01'],'</a></p></td>';
    	 echo '</tr>';
    
    }
    Thanks,
    Mario
    $ Available For Hire
    Need a website designed or coded? Then feel free to contact me!

    PSP Backgrounds - PSPBG.net || Photoshop Brushes - PSBrushes.net

  2. #2
    SitePoint Addict CWebguy's Avatar
    Join Date
    Mar 2009
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried a where clause, like maybe you could combine a DISTINCT Mysql command with a where clause to only update if the criteria is met.

    Cheers.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code MySQL:
    INSERT IGNORE INTO table_name 
             (col1,col2,col3,col4)
             (select t.col1
                   , t.col2
                   , t.col3
                   , t.col4
              from table_one t
              where t.col1 = ?
              and t.col2 = ?  
              and t.col3 = ? 
              and t.col4 = ?

    substitute the ? with your values

    I can't recall but, I think it may be useful to have a unique index on the referenced columns

    hth

    bazz

  4. #4
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Set the column you do not want to have repeat values in as UNIQUE. No duplicate entries will be recorded.
    Code SQL:
    ALTER TABLE directors ADD CONSTRAINT UNIQUE (column_name);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •