DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Michael has posted 27 posts at DZone. View Full User Profile

Multi Select Box To MySql

12.21.2006
| 554 views |
  • submit to reddit
        // Generate multi select box from data in db, then when submitted, each selection is written to a db table as it's own record, each tied together by a common record id.

<? 
//Create multi select box, pulling data from db
		$sql = "SELECT system_id,system_name FROM csr_system order by system_name ASC";
		$result = mysql_query($sql);
		
		if($result && mysql_num_rows($result)>0)
		{
		?>
		<select name="system_id[]" size=5 multiple><option value=0 selected>Select System...</option>
		<?
		for($i=0;$i<mysql_num_rows($result);$i++)
		{ 
		$arr=mysql_fetch_array($result);
		echo "<option value=" . $arr['system_id'] . ">".$arr['system_name'];
		}
		echo "</select>";
		}else
		echo "No Categories in DB";
		?>

<?
//when multi select is submitted from form, this part processes it

$system_id = $_POST['system_id'];

foreach ($_POST['system_id'] as $system_id)
	
	$result_system = mysql_query("INSERT INTO csr_assigned_system (csr_id, system_id) VALUES ('$csr_id','" . $system_id . "')")or die("Insert Error: ".mysql_error()); 

?>