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:
$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:
<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:
$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.