如果while循环中的语句生成具有默认值的下拉列表,则在更新外键时会导致SQL错误

The problem I'm having is that the following code causes a MySQL error when attempting to modify a record.

        $selected_value = mysql_query("SELECT product_categories_ID FROM webstore.products WHERE ID = $PROD_NUM"); 
        $sql = mysql_query("SELECT * FROM webstore.product_categories"); 
            while ($row = mysql_fetch_array($sql)){ 
                if($row['ID'] = $selected_value) 
                    {
                        echo "<option selected='selected' value='". $row['ID']."'>" . $row['NAME'] . "</option>";
                    }
                else
                    {
                        echo "<option value='". $row['ID']."'>" . $row['NAME'] . "</option>"; 
                    }
        }

The above code is intended to list categories from a related table and use the name and ID attributes to populate a drop down box where the user see's the name but the value passed is the ID. The if statement is to determine the default value.

The SQL error given is:

Cannot add or update a child row: a foreign key constraint fails (webstore.products, CONSTRAINT fk_products_product_categories FOREIGN KEY (product_categories_ID) REFERENCES product_categories (ID) ON DELETE NO ACTION ON UPDATE NO ACTION)

I have traced the error back to this section of code but have no clue how to rectify it.

$selected_value has the result resource that is being evaluated.

Try this:

$result = mysql_query("SELECT product_categories_ID FROM webstore.products WHERE ID = $PROD_NUM"); 
$row = mysql_fetch_row($result);
$selected_value = $row["product_categories_ID"];

Your if condition should have ==

I think you should also check you update query.