Hello all!

I currently have a drop down which consists of a list of names generated from the database. My problem is that when selecting a name from the dropdown I would like to insert the name and the corresponding ID into the database table. I am not sure if this is possible with php?

I would appreciate any help.

Here is the code which I made so far.
This populates the dropdown with the names and when processing the form, it inserts the name into the name column. I would just like to add the ID into the ID column which goes along with the chosen name.

$sql = "SELECT birds.bird_id, birds.name FROM birds"; 
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
<label>Select the bird you have spotted*:</label> 
<select name="birdname">
<option value="<?php echo $row['bird_id'] , $row['name']?>"><?php echo $row['name'] ?></option>
	while ($row = mysql_fetch_assoc($result)) { ?>
		<option value="<?php echo $row['name'] ?>">
		<?php echo (isset($_POST['name']) && $_POST['name'] == $row['bird_id'] ? ' selected="selected"' : '');?>
<?php echo $row['name']?></option>
<?php } ?>
5 Years
Discussion Span
Last Post by Biiim
Featured Replies
  • 1

    don't forget to trim the new_data.. [CODE]$birdName = trim($new_data[0]); $birdId = trim($new_data[1]);[/CODE] Read More



For this

<select name="birdname"><option value="<?php echo $row['bird_id'] , $row['name']?>">

I would $_POST it first to get the value from the <option>, and then explode it by " ' "

Something like this.. codes below are example and should be modified, before implementing to production server..

$bird_name_id = $_POST['birdname'];
## when posted the value of $bird_name_id is something like this name , id 
## this is where we need to use the explode function or any handy php function available.. your choice.
$new_data = explode(",", $bird_name_id);
$birdName = $new_data[0];
$birdId = $new_data[1];

Edited by veedeoo: n/a


don't forget to trim the new_data..

$birdName = trim($new_data[0]);
$birdId = trim($new_data[1]);

Theres no need to put the birds name in the value, always use it's id to reference it

<option value="<?php echo $row['bird_id'];?>"><?php echo $row['name'];?></option>

the id is the bird and all its data including the name, no need to store it anywhere else



Thanks for all the input everyone.

It seems like this explode function is the one I am looking for.

I have placed the explode code into the form processing php, however I am getting the error "Notice: Undefined offset: 1...." when I press submit.

Not quite sure on how to implement this properly as I never had to deal with a situation like this before.

Ideally it should work as once the ID and the Name are split I can just insert the two different variables into their columns in the database.

Anyone help me out a bit further please?


Whats the code in your process page up to where it explodes? - no pun intended

undefined offset: 1 makes it sound like there isnt a comma in the post'ed var, ie no comma is found so only creates 1 variable with the full string.

Try running a var_dump($bird_name_id); just before the explode command, maybe the comma has been canonicalised

<?php session_start();
include ('includes/functions.php');
			//protect and then add the posted data to variables
			$birdDetails = $_POST['birdname'];
			$birdname_id = explode(",", $birdDetails);
			$birdName = trim($birdname_id[0]);
			$birdId = trim($birdname_id[1]);
			$description = protect($_POST['description']);
			$latitude = protect($_POST['latitude']);
			$longitude = protect($_POST['longitude']);

This is how the beginning of the process php looks. Followed by some validation of certain fields and then finally the information is inserted into the database.

Running the form as it is right now shows that the name is inserted, but the ID is still not inserted.

Not sure what is wrong with it as there is a comma present in the code I showed off at the beginning (which I haven't touched since)


Also, after running the var_dump this is the result I get.

string 'Balearic Shearwater' (length=19) (name of selected bird only , no ID shown)


One thing i notice on the form is theres actually no comma outputted on the page:

<select name="birdname">
<option value="<?php echo $row['bird_id'] , $row['name']?>"><?php echo $row['name'] ?></option>

this should be

<select name="birdname">
<option value="<?php echo $row['bird_id'].','.$row['name']?>"><?php echo $row['name'] ?></option>

also once you've done this look at the source of the form and check the html on the select box, it should say something like:

<option value="1,heron">heron</option>

Once it says that in the value we're good to move onto fixing the process page - if it still errors after that.


Right, I still had the same error coming up regarding the comma issue, so I decided to simplify my PHP code to the following:

Now it is:

$result = mysql_query("SELECT birds.bird_id, birds.name FROM birds") or die(mysql_error());
echo "Select the bird you have spotted*: <br />";
echo '<select name="birdname" id="birdname">';
while ($row= mysql_fetch_assoc($result)) {
echo '<option value="' . $row['bird_id'] . ',' . $row['name'] . '">' . $row['name'] . '</option>';
echo '</select>';

Now everything seems to work just how I want it to.

Thanks for the help!



Remember to mark the thread as solved, it appears in a paragraph just above where you type your reply/message.

It lets the helpers know they don't need to check it out and spend their time reading/answering something that's already solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.