title: PHP/MYSQL - Inserting multiple rows using mysql_insert_id?
body: <h2><strong>BACKGROUND:</strong></h2>
  
  <p>I am uploading basic client information to a database from a client form. When the client clicks "Finished" the information entered such as <em>(Name</em>, <em>City</em>, <em>Country</em> etc.) are stored in the <strong>client_info</strong> table.</p>
  
  <p>As an alternative the client can also upload image(s) to the server. The information of these images (<strong>image_path</strong>, <strong>image_name</strong>) are then stored in another, separate table called <strong>client_images</strong>, which has a <em>Foreign Key</em> linked with <strong>client_info</strong>. </p>
  
  <p><strong>client_info table:</strong></p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `client_info` (
    `client_id` int(11) NOT NULL AUTO_INCREMENT,
    `client_name` varchar(100) NOT NULL,
    `client_city` varchar(100) NOT NULL,
    `client_country` varchar(10) NOT NULL,
    PRIMARY KEY (`client_id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10446 ;
  </code></pre>
  
  <p><strong>client_images table:</strong></p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `client_images` (
    `image_id` int(11) NOT NULL AUTO_INCREMENT,
    `image_path` varchar(100) NOT NULL,
    `image_name` varchar(100) NOT NULL,
    `client_id` int(11) NOT NULL,
    PRIMARY KEY (`image_id`),
    KEY `client_id` (`client_id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=415;
  </code></pre>
  
  <hr>
  
  <h2><strong>PROBLEM:</strong></h2>
  
  <p>I have managed to successfully upload one row to the <strong>client_images</strong> table. Adding more than one row results in the following <strong>error</strong>:</p>
  
  <pre><code>Cannot add or update a child row: a foreign key constraint fails (`db`.`client_images`, CONSTRAINT `client_images_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `client_info` (`client_id`) ON DELETE CASCADE ON UPDATE CASCADE) : 1452
  </code></pre>
  
  <p>However, when I test it through the console adding multiple rows and an already existing <strong>client_id</strong> it works perfectly well.</p>
  
  <p>Example: </p>
  
  <pre><code>mysql&gt; INSERT INTO client_images (image_path, image_name, client_id) VALUES ('pathname1', 'name1', 10241);
  Query OK, 1 row affected (0.00 sec)
  
  mysql&gt; INSERT INTO client_images (image_path, image_name, client_id) VALUES ('pathname2', 'name2', 10241);
  Query OK, 1 row affected (0.00 sec)
  
  mysql&gt; INSERT INTO client_images (image_path, image_name, client_id) VALUES ('pathname3', 'name3', 10241);
  Query OK, 1 row affected (0.00 sec)
  </code></pre>
  
  <p>I need to point out that I have it lined up like this:</p>
  
  <p>File: <strong>(create_article.php)</strong></p>
  
  <pre><code>insert_client_info($client_name, $client_city, $client_country);    
  
  include('uploads.php');
  
  ?&gt;
  
  &lt;?php mysql_close($connection); ?&gt;
  </code></pre>
  
  <p>Inside <strong>uploads.php</strong> I have a <em>foreach</em> loop that counts how many files that are going to be uploaded and in the end of the loop I currently have a function <strong>insert_images($image_path, $image_name);</strong></p>
  
  <p><strong>FUNCTION (insert_images):</strong></p>
  
  <pre><code>function insert_images($directory_path, $image_name) {  
  
      global $connection;
      $client_id=mysql_insert_id();
  
      $query = "INSERT INTO client_images (
          image_path, image_name, client_id
      ) VALUES (
          '{$directory_path}', '{$image_name}', '{$client_id}'
      )";
  $result = mysql_query($query, $connection);
  
  if ($result) {
      // Success!
      echo "&lt;p&gt;&lt;/p&gt;";
      echo "Successfully uploaded the file.";
      echo "&lt;p&gt;&lt;/p&gt;";
  
  } else {
      // Display error message.
      echo "&lt;p&gt;" . mysql_error() ." : ".mysql_errno(). "&lt;/p&gt;";
  }
  return $result;
  }
  </code></pre>
  
  <p>Reading other similar problems I understand that the Achilles heel is probably <strong>mysql_insert_id()</strong> and that somehow it uses another connection different from the first one in the <em>foreach</em> loop, thus resulting in getting the before mentioned MYSQL error. </p>
  
  <p>Thanks
  /
  EDZ</p>