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

 

Link to comment
Share on other sites

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'");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.