add subcategoryto category based on selection of category

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
angelic_devil
Forum Commoner
Posts: 74
Joined: Thu Apr 02, 2009 7:05 am

add subcategoryto category based on selection of category

Post by angelic_devil »

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

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>
someone plz help
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: add subcategoryto category based on selection of categor

Post by Jade »

Check to make sure you have a value in $category_select when you try to add a new sub category to the database. You're not passing the $categoryname in the form action of the sub-category so it doesn't have a value when it tries to add the sub-category to the database.

Code: Select all

<form action="addsub.php?categoryname=$_GET['categoryname']" method="post">
Post Reply