title: PHP and MYSQL: Inserting Foreign Key
body: <p>I have two tables, IMAGES and CATEGORIES.</p>
  
  <p>CATEGORIES to IMAGES is a 1:M relationship (many images can have 1 category).</p>
  
  <p>I'd like to insert images into a database from the filesystem, dynamically assigning foreign key categories on the way in.</p>
  
  <p>I'd like to achieve this with a single insert, but it appears I need an insert for each category of image, like so:</p>
  
  <pre><code> $allimages = scandir('./images/all_images/');
   $category1= scandir('./images/category1/');
   $category2= scandir('./images/category2/');
  
  //CATEGORY 1: 
  for($x=0; $x&lt;count($category1); $x++)
  {   
  if(!is_dir(IMAGES_PATH . $category1[$x]))   
  {  
    INSERT INTO images (imgid, imgname, categoryfk) VALUES ('$x', '$category1', 1)
  
  //CATEGORY 2: 
  for($x=0; $x&lt;count($category2); $x++)
  {   
  if(!is_dir(IMAGES_PATH . $category2[$x]))   
  {  
    INSERT INTO images (imgid, imgname, categoryfk) VALUES ('$x', '$category2', 2)
  
  //CATEGORY 3: 
  for($x=0; $x&lt;count($allimages); $x++)
  {   
  if(!is_dir(IMAGES_PATH . $allimages[$x]))   
  {  
    INSERT INTO images (imgid, imgname, categoryfk) VALUES ('$x', '$allimages', 3)
  </code></pre>
  
  <p>Is this really the only way I can do this? Can I achieve this category assigning in a single insert and loop?</p>
  
  <p>Thank you!</p>