category and subcategory help


#1

hi I’m made a list for the category and the sub category

but on the one that i did the sub category is allways the same for both the categories

I know this because i didn’t do it in the code, i’m not sure how to do it so please help

the category and the sub category are stored in the database when you pick them and submit

this is the code:

<tr> <td align="right">Category</td> <td><label> <select name="category" id="category"> <option value="cate1">Heating</option> <option value="cat2">Plumbing</option> </select> </label></td> </tr> <tr> <td align="right">Subcategory</td> <td><select name="subcategory" id="subcategory"> <option value="<?php echo $subcategory; ?>"><?php echo $subcategory; ?></option> <option value="Hats">cat1.sub1</option> <option value="Pants">cat1.sub2</option> <option value="Shirts">cat2.sub1</option> </select></td> </tr>

this is the php

[php] <?php
// Parse the form data and add inventory item to the system
if (isset($_POST[‘product_name’])) {

$pid = mysql_real_escape_string($_POST['thisID']);
$product_name = mysql_real_escape_string($_POST['product_name']);
$price = mysql_real_escape_string($_POST['price']);



$category = mysql_real_escape_string($_POST['category']);
$subcategory = mysql_real_escape_string($_POST['subcategory']);




$details = mysql_real_escape_string($_POST['details']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("UPDATE products SET product_name='$product_name', price='$price', details='$details', category='$category', subcategory='$subcategory' WHERE id='$pid'");
if ($_FILES['fileField']['tmp_name'] != "") {
// Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
}
header("location: inventory_list.php");
exit();
}
?>[/php]

#2

Not sure what you are asking. I will guess.

You have a list of categories.
You select one.

The subcategories should changed based on the categoy that was selected.

Is that what you want? If so, it can not be done the way you are doing it now.
The category must be selected and that option must be used to pull the correct list of subcategories.
So, the SQL for pulling the subcategories will have to include the category in the WHERE clause.

Do you understand that? If so, there are a few ways to fix your code. The easiest is to refresh the page by calling it again with the category set and then pulling the sub-category using the category’s value in the SQL.
Try it and if yo can not figure it out, post what you have at that point and we can help you.