Jump to content

multiple selected items from dropdownlist inserting in table


paulus4605

Recommended Posts

I have created this multiple form

de hond is behandeld tegen:<?php

mysql_connect("127.0.0.1", "root", "pass")or die("cannot connect");

mysql_select_db("tobysplace")or die("cannot select DB");

$query="SELECT * FROM vaccination";



/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */



$result = mysql_query ($query);

echo "<select multiple name=vaccination[] value=''><option>de hond is behandeld tegen:</option>";

// printing the list box select command



while($nt=mysql_fetch_array($result)){//Array or records stored in $nt

echo "<option value=".$nt['vaccination_id'].">".$nt['vaccination_name']."</option>";

/* Option values are added by looping through the array */

}

echo "</select>";// Closing of list box

?>

everything is stored in a session with $_SESSION['vaccination'] = $_POST['vaccination'];

how do I get each unique id from each selected item inserted in the table dog_vaccination where 2 foreignkeys are held

 

which is dog_id and vaccination_id as foreignkeys.

 

thanks for your help

 

is something like this what you're after?

 

you need to enclose the select name in quotes;

echo "<select multiple name='vaccination[]' value=''><option>de hond is behandeld tegen:</option>";

 

and then when you process the form (this assumes the dog's id is stored in the session under the session variable dog_id

if (is_array($_POST['vaccination'])) {
foreach ($_POST['vaccination'] AS $vaccination) {
$dog = $_SESSION['dog_id'];
@mysql_query("INSERT INTO vaccination SET
vaccination_id='$vaccination',
dog_id='$dog'");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.