# Assigning the foreign key and a variable in the same selection

**URL:** <https://forum.kirupa.com/t/assigning-the-foreign-key-and-a-variable-in-the-same-selection/243837>\
**Category:** Uncategorized\
**Created:** [November 11, 2007, 6:05pm UTC](https://forum.kirupa.com/t/assigning-the-foreign-key-and-a-variable-in-the-same-selection/243837 "2007-11-11T18:05:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaplanyo](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kaplanyo](https://forum.kirupa.com/u/kaplanyo)\
**Post date:** [November 11, 2007, 6:05pm UTC](https://forum.kirupa.com/t/assigning-the-foreign-key-and-a-variable-in-the-same-selection/243837/1 "2007-11-11T18:05:56Z")

</div>

Here’s my problem. Any guidance would be much appreciated. I’ll try and describe it the best way I can, it seems so simple, but i’m lacking on the code skill.

I’ve got a list of ‘items’ and those ‘items’ have unique ‘categories’.  
I have 1 table for ‘categories’ that contains a list of category\_ids and their ‘category\_names’. The other table is the ‘item’ list. And here’s where I’m running into difficulty, I’d like to have the ‘item’ table contain the category\_id from my ‘categories’ table as a Foreign Key and that category\_id’s ‘category\_name’ in a ‘category’ field. I’m getting the Foreign Key, but I can’t seem to grab the ‘category\_name’ and assign it to my $category variable in the ‘item’ table.

I’ve got a page to insert items and a page to update these items. I was working with the update first. And I’m able to grab the list of categories and select the correct one, but I’m not seeing where to assign the category\_name to the $category variable… the category\_id is in use as for the foreign key assignment. Here’s the code I have so far:

Here’s what the UPDATE looks like in the POST:

```php
$sql = "UPDATE dinner SET category_id = $category_id, category = '$category', description = '$description', additional = '$additional'
          WHERE item_id = $item_id";

```

Here’s what the select looks like in the form:

```auto
<p>
      <label for="category_id">Menu category:</label>
      <select name="category_id" id="category_id">
        <option value="">Select category dude</option>
		<?php
		//get details of categories
		$getCategories = 'SELECT * FROM dinner_categories ORDER BY category_id';
		$categoryList = mysql_query($getCategories) or die (mysql_error());
		while ($categories = mysql_fetch_assoc($categoryList)) {?>
        <option value="<?php echo $categories['category_id'];?>"
		<?php if ($categories['category_id'] == $row['category_id']) {
		  echo ' selected="selected"';
		  }?>><?php echo $categories['category_name']; ?></option> 
		  <?php } ?>
      </select>
    </p>

```

It seems like I could say something like:

```php
$category = $categories['category_name'];

```

But I tried that in a few places and it didn’t work. I was able to make to selection dropdowns and have one assigned for the foreign key and the other for the $category, using the \_id for 1 and the \_name for the other.
