Learner
Learner

Reputation: 621

How to insert the value of multiple drop-down menus in PHP

I have a page to insert meeting details, one of the sections of this insert form is selecting the attendance from existing list of members in db. My question is: How to insert many drop-down menus (max number of menus is equal to the total number of members) in mysql db. This way works great in other scenario that I have done the only difference that it was an input type of text instead of select menu So What did I miss here ?

Bellow all the code:

addMeeting.php

DB

Upvotes: 1

Views: 162

Answers (3)

Learner
Learner

Reputation: 621

I changed my scenario to this: instead of having many drop down lists I created one select of multiple values and I could manage getting an array of the user selected values and insert them successfully ^^

So Don't complicate your code > Hard lesson :)

Upvotes: 1

Ali Arshad
Ali Arshad

Reputation: 436

can you show me the code where you are inserting a new meeting in boardmeetings. I believe you are doing it just before if (isset ($_POST['attendence'])&& $_POST['attendence']!='none')

is this you full addMeeting.php code?

<?php
$dbhost="XXX";
$dbuser="XXX";
$con = mysql_connect($dbhost,$dbuser, "XXX");
if (!$con) 
{
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

$query = "SELECT * FROM `boardteam` ORDER BY fName ASC";
mysql_select_db('dahBoard');
$result = mysql_query($query);
if ($result)
{
   while($db_field = mysql_fetch_assoc($result))
     {
     $Attendence .= "<option value=".$db_field['nationalID'].">".$db_field['titleName']."."." ".$db_field['fName']." ".$db_field['sName']." ".$db_field['lName']."</option>";
     }
 }

if(isset($_POST['add']))
{
   if (isset ($_POST['attendence'])&& $_POST['attendence']!='none') {
    for ($i = 0; $i < count($_POST['attendence']); $i++) {
    $new_attendence = $_POST['attendence'][$i];

    $query = "INSERT INTO `meetingattendance`(`meetingID`, `attendance`) 
              VALUES (LAST_INSERT_ID(),'$new_attendence')";

    mysql_query($query) or die('Error, query failed');
    }
}
}
?>

Upvotes: 2

Federico
Federico

Reputation: 1251

Update:

If I understand:

You wont to write how many attendance there are for a specific meeting right ?

if this is your case you should already know your "meetingID".

// for example ...
$meetingID = $_POST['meetingid']; 
$reruslt =& mysql_query('INSERT INTO meetingattendance (meetingID, attendance) VALUES ('.$meetingID.', "'.$new_attendence.'")', $conn);
if(!$reruslt) die( 'mySQL err. '.mysql_errno($conn).': '.mysql_error($conn) );

LAST_INSERT_ID() only return the last inserted auto increment key, in this case "boardmeetings.meetingID".

CONSTRAIN FOREIGN KEY make sure that you cannot insert a meetingID in "meetingattendance" that does not exists in "boardmeetings".

You should use LAST_INSERT_ID() only directly after an insert in "boardmeetings".

MYSQL:

INSERT INTO `boardmeetings`(`meetingID`) VALUES ('');
@lastID = LAST_INSERT_ID();
INSERT INTO meetingattendance (meetingID, attendance) VALUES (@lastID, 'new_attendence');

Or in PHP

$reruslt =& mysql_query('INSERT INTO boardmeetings (meetingID) VALUES ('')', $con);
$lastID = mysql_insert_id($conn);
$reruslt2 =& mysql_query('INSERT INTO meetingattendance (meetingID, attendance) VALUES ('.$lastID.', "'.$new_attendence.'")', $conn);
if(!$reruslt2) die( 'mySQL err. '.mysql_errno($conn).': '.mysql_error($conn) ); 

Upvotes: 2

Related Questions