Hello- I have an update form where I'm trying to populate a SELECT multiple field with a list of 48 categories, from tbl work_cat. And show, as SELECTED, the one or many choices that the user had previously selected from the 48 categories which are stored in tbl cat_relations as $relation_cat. Then allow the user to update their selections and update the database.
My code below is showing the previous selections highlighted in the drop-down box, and submitting the form seems to work (I receive the "GOOD" message) and there are no errors. But the data in the table cat_relations is not updated correctly, that is: the wrong category id's etc.. Below are my form page and my processing page. What do I have wrong? Any help would be appreciated.
Thanks.
//my form
<?php
include ('../connect.php');
$ID = '37';
$c = array();
$sql = "SELECT * FROM cat_relations where relation_usr_id = '$ID' ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$c[$row["relation_cat"]] = " selected " ;
}
$sql2 = "SELECT * FROM work_cat ORDER BY cat_name ASC";
// execute SQL query and get result
$result = mysql_query($sql2);
?>
<table width="60%" align="center">
<td>
<form name=addcats action=do_updatecats.php?ID=<?php echo $ID; ?> method=POST>
<p>Select work categories. Hold down the Ctrl key to make multiple selections.
<p>
<SELECT name="co_workcat[]" size="4" multiple>
<?php
while ($row = mysql_fetch_array($result)) {
$i = $row["cat_name"];
$strCatId = $row['ID'];
echo '<OPTION value="' . $strCatId .' "'. $c[$strCatId]. '>' . $i . '</OPTION>';
}
?>
</select>
<p><input type=submit name=submit value=SUBMIT>
</form>
// my processing page - do_updatecats.php
<?php
include ('../connect.php');
$ID = $_GET['ID'];
$cats = $_POST['co_workcat'];
foreach($cats as $cat) {
$cat_list1 .= $cat . ' ';
}
;
$query = mysql_query("SELECT ID FROM work_cat where cat_name = '$cat_list1'");
$result = $query;
while ($row = mysql_fetch_array($result)) {
echo ($row["ID"]);
$cat_list1 = $row["ID"];
};
foreach ($cats as $strKey => $strValue)
{
$sql = "UPDATE cat_relations SET relation_usr_id='$ID',relation_cat='$strValue'";
if (@mysql_query($sql)) {
echo ("GOOD");
} else {
echo("<p>Error adding cats: " .
mysql_error() . "</p>");
}
}
?>