Hi there. I'm having a problem getting values from a drop down to insert into a table. What I want to do is get the key for the value and then insert that. For example: I have a table called format which holds an auto incremented ID key and a format type, the format types are populated to a drop down and the user can pick one. But what I need in the INSERT isn't the format type but its ID and for some reason all I'm getting is zero's. The format table is prepopulated, meaning I've gone in and put values in it myself without the use of php or anything since I don't plan on a user being able to edit this table. Here's some code.

<tr>
<td>Format:</td>
<td>	
<?php 
								if(mysql_num_rows($format)){ 
											       $select= '<select name="format">';   
											while($rs=mysql_fetch_array($format)){ 
												$select.='<option value="'.$rs['formatID'].'">'.$rs['formatType'].'</option>'; 
		} 
	} 
										$select.='</select>'; 
	echo $select;
?> </td>
</tr>

Sorry for the state of it, notepad++ tabbing doesn't seem to copy and paste well

$format=$_POST['format'];


	
	$formatID = mysql_query("select formatID from format where formatType = '$format'") or die(mysql_error());

but the select statement doesn't seem to be returning the value properly or something. In order to get this to work with a few non-drop down fields I had to do it this way:

mysql_query("insert into authors(authorName, authorBio) values('$author', '$bio')") or die(mysql_error());
	
	//$authorID = mysql_query("select authorID from authors where authorName = '$author' and authorBio = '$bio'") or die(mysql_error());
	
	$authorID = mysql_insert_id();

which gives me the proper value right now, though I'm thinking this might cause me some errors when I try to avoid duplicating entries.

You confused formatID and formatType. Instead of

$formatID = mysql_query("select formatID from format where formatType = '$format'") OR die(mysql_error());

code

$formatID = mysql_query("select formatID from format where formatID = '$format'") OR die(mysql_error());

I'm trying to get the formatID, if $format = formatID I wouldn't have a problem :D

the drop down menu lists formatTypes and what I need is the key for that value, the formatID. I'm assuming the value passed to $format would be the formatType selected from the menu

Your assumption is wrong. The value posted from a <select> field is what you code in the value attribute of the selected option. Only if there is no value attribute, the text node value of this option element will be sent as the select value.

oooh! somewhere I think I learned this before and forgot it. *makes a quick edit* And yay! that works now! Thanks!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.