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
, CONSTRAINTfk_products_product_categories
FOREIGN KEY (product_categories_ID
) REFERENCESproduct_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.