situation:
i have a website where i have 3 database tables
items - categories - list
items = the items of course (tuit - fred - apple)
categories = my categories (red - yellow - 5 - tall)
list = the thread between the 2 other tables
table breakdown
items - ID (auto increment), Title, Description
categories - ID (auto increment), Title, Description
list - ID (auto increment), Item Title, Category ID
my webpage submission form (for adding an item to the database) has this line
<input type=\"checkbox\" name=\"".$cat_id."\" value=\"".$cat_id."\" /> $cat_title <br />
which provides this
[ ] cat desc 1
[ ] cat desc 2
[ ] cat desc 3
and on and on for each category description
effectively giving a checkbox for each category (so that i can add an item to more than 1 category)
i am trying to submit this to my database, but i get a small problem - if i use a variable name ".$cat_id." in the <input> tag then i cant submit it to my database with
$query = mysql_query
("INSERT INTO list VALUES('$next_id','$_POST[category_id]','$_POST[item_title]')") or die(mysql_error());
as the category id would be variable
but if i make the name static <input name"cat_id"> then it will only pass the LAST checked box into the database..
i am willing to rewrite the whole database as necessary, the only things in it are test items so far anyways
anyone have a cleaner way to do this, or an idea on how i can make this work?
Thank you all in advance for any assistance provided
-Kender
here is the whole page for refrence
<?
include_once("Subs.php");
global $settings;
doheader('Item Added');
if ($_POST['item_title'] == "")
fatal_error("The item title is required. Please go back and try again. <br /><br />
« <a href='javascript:history.back(1)'>Back</a>",1);
if ($_POST['item_desc'] == "")
fatal_error("The item description is required. Please go back and try again. <br /><br />
« <a href='javascript:history.back(1)'>Back</a>",1);
$_POST['item_title'] = addslashes($_POST['item_title']);
$_POST['item_desc'] = addslashes($_POST['item_desc']);
$slq = 'SELECT max(id) as current_id FROM items';
$id_result = mysql_query($slq);
$id_row = mysql_fetch_array( $id_result );
$next_id = $id_row['current_id'] + 1;
$query = mysql_query
("INSERT INTO items VALUES('','$_POST[item_title]','$_POST[item_price]','$_POST[item_desc]')") or die(mysql_error());
$query .= mysql_query
("INSERT INTO list VALUES('$next_id','$_POST[category_id]','$_POST[item_title]')") or die(mysql_error());
$get_cats = "select id, cat_title from categories";
$get_cats_res = mysql_query($get_cats) or die(mysql_error());
if (mysql_num_rows($get_cats_res) < 1)
{
$display_cats = "<p><i>Sorry, no categories set up.</i></p>";
} else {
while ($categories = mysql_fetch_array($get_cats_res))
{
$cat_id = $categories[id];
$cat_title = stripslashes($categories[cat_title]);
$display_cats .= "
<input type=\"checkbox\" name=\"category_id.\" value=\"".$cat_id."\" /> $cat_title <br />";
}
}
echo "Your new item has been entered successfully...<br /><br />Enter another item?";
?>
<table align="center" cols="2" border="0" width="380" cellpadding="0" cellspacing="0">
<form action="additem.php" method="post" "enctype="multipart/form-data>
<tr>
<td valign="center" align="right" class="me" width="160">
<br />Item Title:
</td>
<td valign="center" width="220" class="me">
<br /><input type="text" size="25" align="right" name="item_title">
</td>
</tr>
<tr>
<td valign="center" align="center" colspan="2" class="me">
<br />Item Description:
<br /><textarea cols="40" rows="5" name="item_desc"></textarea>
</td>
</tr>
<tr>
<td></td>
<td class="me"> <? echo $display_cats; ?>
<br><input type="submit" value="Submit"> <input type="reset" value="Reset"></form>
</td>
</tr>
</table>
<?
if($_GET['action'] == "delete"){doDelete();}
else
{
$display_block = "<br /><center><hr width=\"300\">Current existing items.<hr width=\"300\"><br /><br />
</center><br /><hr align=\"left\" width=\"600\" />";
$get_items = "select id, item_title, item_price, item_desc from items order by item_title";
$get_items_res = mysql_query($get_items) or die(mysql_error());
if (mysql_num_rows($get_items_res) < 1)
{
$display_block = "<p><i>Sorry, no items to browse.</i></p>";
} else {
while ($items = mysql_fetch_array($get_items_res))
{
$item_id = $items[id];
$item_title = stripslashes($items[item_title]);
$item_desc = stripslashes($items[item_desc]);
$display_block .= "<table><tr valign=\"top\"><td rowspan=\"3\">
<a href=\"./../images/gif/".$item_title.".gif\" target=\"_new\">
<img src=\"./../images/jpg/".$item_title.".jpg\" width=\"100\" height=\"100\" border=\"0\"></a>
</td><td class=\"me\" width=\"200\"><b>$item_title</b></td>
<td class=\"me\" width=\"200\" align=\"right\"><a href=\"./addaitem.php?action=delete&ID=$item_id\">
Delete</a></td></tr><tr valign=\"top\">
<td class=\"me\" colspan=\"2\">$item_desc </td></tr><tr valign=\"bottom\"><td class=\"me\">
Categories: $categories (will be set up once i get the data inputed)</td></tr></table><hr align=\"left\" width=\"600\" />";
}
}
echo $display_block;
}
function doDelete(){
$query = mysql_query("DELETE FROM items WHERE ID='$_GET[ID]'") or die(mysql_error());
$query .= mysql_query("DELETE FROM list WHERE ID='$_GET[ID]'") or die(mysql_error());
header("Location: ./addaitem.php");
}
dofooter();
exit;
?>
(pardon how sloppy it is, i did edit it so that it wouldnt scroll the screen - on a 1280 resolution screen - i apologize for the scrolling on the smaller resolutions)
and again, thank you in advance for helping me in this situation