add subcategoryto category based on selection of category
Posted: Mon Jul 04, 2011 3:20 pm
i m population the dropdown box with list of category from table categorylist and then i m trying to add subcategory (entered in text field) and category (selected in the dropdown box) to table subcategory
currently it adds the subcategoryname but leaves the categoryname blank in the table subcategory
someone plz help
currently it adds the subcategoryname but leaves the categoryname blank in the table subcategory
Code: Select all
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/form_functions.php"); ?>
<?php
//------------------------------------------------------------
// Part 1: Choosing a category
//============================================================
$subcategoryname="";
$category = "";
// Initializes a list of acceptable category
$categoryname_list = array();
$query_category = " SELECT categorylist.categoryname FROM categorylist ";
$result_category = mysql_query($query_category);
confirm_query($result_category);
$categoryname_list[]=" ";
while ($record = mysql_fetch_assoc($result_category)) {
$categoryname_list[] = $record['categoryname'];
}
?>
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<select name="categoryname" >
<?php
$category_select = $_GET['categoryname'];
// Loops through the $category_list array
foreach ($categoryname_list as $value => $option)
{
// Sets an attribute to show the chosen category as selected
$selected = ($category_select == $value) ? ' selected="selected"' : '';
// Builds an option for each acceptable category
echo '<option value="'.$value.'"'.$selected.'>'.$option.'</option>';
}
?>
</select>
</form>
<?php
//-----------adds subcategory to database ---------------------
if (isset($_POST['addsubcategory'])) {
$subcategoryname=$_POST['subcategoryname'];
if ($subcategoryname=="")
{ $message = "you cannot add a blank sub category";}
else{
$query_subcat = "INSERT INTO subcategory ( categoryname,subcategoryname ) VALUES ('$categoryname_list[$category_select]','$subcategoryname')";
$result_subcat = mysql_query($query_subcat, $connection);
if ($result_subcat) {
$message = "new subcategory has been added ";
}
else {
$message = "new subcategory could not be to the list";
}
}
}
?>
<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
<form action="addsub.php" method="post">
<table>
<tr>
<td>subcategory name</td>
<td><input type="text" name="subcategoryname" maxlength="200" value="<?php echo htmlentities($subcategoryname); ?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="addsubcategory" value="addsubcategory" /></td>
</tr>
</table>
</form>
</body>