I have a parent table and a child table linked with an Id. The child table has two fields - ch_id and item. The parent table has a field which stores the value of ch_id for that row. I have set up a php script to generate a selection list in an html page.
The selection list only displays the text values of 'item'. Following a Submit the $_POST value will contain the text of the selected item. However, I want to store the ch_id value corresponding.
A couple of ways that I can see, that I do not like, are - display the ch_id and the item text in the selection list and strip the id out before storing. This looks messy on the form; or do a reverse mySQL selection before updating the parent table.
Is there a smarter way to achieve this? In MS Access the list box can contain both fields with the id not displaying but being linked to the parent field.

Member Avatar

I believe there's a way for you to not display the ch_id on the form anymore while querying the database but directly assign it to a different variable ready for storage. You might find using <input type="hidden"> helpful in some cases.

If you can show us a sample of your code that would be great.

//get Branch data
$get_list = "SELECT * FROM drop_downs WHERE type = '2' ORDER BY text";
$get_list_branch = mysql_query($get_list) or die(mysql_error());

<td><select name="branch">
while ($recs_branch = mysql_fetch_array($get_list_branch))
echo '<option ' . (($recs_branch[dd_id] == $recs[branch]) ? "selected=selected" : "") . '>' . $recs_branch[dd_id] . " " . $recs_branch[text] . '&nbsp;&nbsp;&nbsp;</option>';

Member Avatar

You need a value attribute linked to DB id in each option.

<option value = "3">text for id3</option>

$_POST will then return the value of the selected option after form submit.

Thanks Ardav - when I look at the documentation, that is the obvious solution.