Forums
This topic is locked
many to many relationship
Posted 12 Nov 2006 21:35:17
1
has voted
12 Nov 2006 21:35:17 Yogesh Mani posted:
Hi Could anyone please help me. I totally confused about inserting / deleting data in the intermediate table in a many to many situation.
tblAdvertisement
AdId(pk)
Offer
Description
DateAdded
tblCategory
CatId(pk)
Category Name
tblAdCat
AdId(pk)
CatId(pk)
i am using this sql query to get the data:
$AdId = $_GET['AdId']
$query = "SELECT tbladvertisement.AdId ,tblcategory.CatId, tblcategory.Category , CASE WHEN tbladcat.CatId IS NULL THEN '0' ELSE '1' END AS selected FROM tbladvertisement CROSS JOIN tblcategory LEFT JOIN tbladcat ON ( tbladcat.AdId = tbladvertisement.AdId AND tbladcat.CatId= tblcategory.CatId) WHERE (tbladvertisement.AdId = '$AdId') ";
$result= mysql_query($query, $crazydeals) or die(mysql_error());
$row = mysql_fetch_assoc($result);
in the form:
form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="left">
<tr><th><span class="style9">Category</span></th>
<th><span class="style9">Selected</span></th>
</tr>
<?php do { ?>
<tr valign="baseline">
<td nowrap align="right"><?php echo ucfirst($row['Category']); ?></td>
<td><input type="checkbox" name="CatId" value="" <?php if($row['selected']==1) {echo "checked";} ?>></td>
</tr>
<?php } while ($row = mysql_fetch_assoc($result)?>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Update Category"></td>
</tr>
</table>
<input type="hidden" name="MM_update" value="form1">
<input type="hidden" name="AdId" value="<?php echo $row['AdId']; ?>">
</form>
------
I don't know how to update the table now. The whole idea was :Checked = Update
and Unchecked = Delete. But i don't how to do it.
Could you please give me some hints as to how i can create a sql that will insert the checked categories and delete unchecked categories.
Kind Regards
Yogesh
YM