Multi Select Box to MySql DB Table
7
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 id,name FROM table ORDER BY name ASC";
$result = mysql_query($sql);
if($result && mysql_num_rows($result)>0)
{
?>
<select name="id[]" size=5 multiple><option value=0 selected>Select Name...</option>
<?
for($i=0;$i<mysql_num_rows($result);$i++)
{
$arr=mysql_fetch_array($result);
echo "<option value=" . $arr['id'] . ">".$arr['name'];
}
echo "</select>";
}else
echo "No Names in DB";
?>
<?
//when multi select is submitted from form, this part processes it
$id = $_POST['id'];
foreach ($_POST['id'] as $id)
$result_system = mysql_query("INSERT INTO diff_table (diff_id,id) VALUES ('$diff_id','" . $id . "')")or die("Insert Error: ".mysql_error());
?>






There are currently no comments for this snippet.