# Multiple MySQL inserts into database

**URL:** <https://forum.kirupa.com/t/multiple-mysql-inserts-into-database/224551>\
**Category:** Uncategorized\
**Created:** [May 3, 2007, 8:13am UTC](https://forum.kirupa.com/t/multiple-mysql-inserts-into-database/224551 "2007-05-03T08:13:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [May 3, 2007, 8:13am UTC](https://forum.kirupa.com/t/multiple-mysql-inserts-into-database/224551/1 "2007-05-03T08:13:37Z")

</div>

hello all.

I have this code which i use to loop through an array and it works great

here it is.  
NOTE: It loops through an array and INSERTS each item into it individually. $item\_id is created by mysql\_insert\_id().

```php

		foreach ($form['material'] as $id){
			$insertQuery = "INSERT INTO cms_item_material (item_id, material_id)
			VALUES ('$item_id', '$id')";
			if (!$result = $connector->query($insertQuery)){
				$warnings[] = 'material '.$id.'';
			}
		}

```

Its not so much as a problem but i was wondering would this way be better as it only outputs one error (if there is one) instead of and array error list.

NOTE: not sure if this the correct syntax but its for the idea.

```php

           $insertQuery = "INSERT INTO cms_item_material (item_id, material_id) VALUES "
		$numberOf = count($form['material']);
		$count = 0;
		while ($count <= $numberOf)
			$insertQuery .= '(\'.'$item_id.'\', \''.$form['material']['$count'].'\')'
			if($count != $numberOf){
				$insertQuery .=',';
			}
		$count ++;
		}
		
		if ($result = $connector->query($insertQuery)){
		return 'items added';
		}

```
