My page has a men that holds categories of products, some of these categoriees have subcategories, some dont.
I have mysql tables category, subcategory and products.
My code right now only adds products to the subcategories, it takes the id value of subcategory and puts it in the product_id field of products. this is my code to do so

include '../../inc/connect.php';

   // file needs to be jpg,gif,bmp,x-png
   if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 8000000)){

     // if uploaded image was JPG/JPEG
      if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
         $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
      // if uploaded image was GIF
      if($_FILES["image_upload_box"]["type"] == "image/gif"){
         $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
      // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
      // if uploaded image was BMP
      if($_FILES["image_upload_box"]["type"] == "image/bmp"){
         $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
      // if uploaded image was PNG
      if($_FILES["image_upload_box"]["type"] == "image/x-png"){
         $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
      $remote_file = "../../images/products/".$_FILES["image_upload_box"]["name"];


               $thumb_height = 150;
               $thumb_width = 150;
               list($image_width, $image_height) = getimagesize($remote_file);
               if($image_width>$thumb_width || $image_height >$thumb_height){
                  $proportions = $image_width/$image_height;
                  $new_height = $thumb_height;
                  $new_width = round($thumb_height*$proportions);
                  $thumb_image = imagecreatetruecolor($new_width , $new_height);
                  $image_source = imagecreatefromjpeg($remote_file);
                  imagecopyresampled($thumb_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
         $cat = intval($_POST['subcat']);
         $aname = $_POST['aname'];
         $aprice= $_POST['aprice'];
         $adescription = $_POST['adescription'];
         $description = mysql_real_escape_string($adescription);
         $name = mysql_real_escape_string($aname);
         $query = "INSERT INTO products VALUES('', '$name', '$description', '$aprice', '$cat', '$remote_file')";
         $result = mysql_query($query);
          echo 'Something went wrong';
         header( 'Location: ../productadd.php' ) ;


How do i modify this so that IF a category has no subcategory then the id from category gets added as product_id in products.

Or if there is a better way to do this?


I am not good in PHP. so I am not looking into your code but i would like to give you some idea so you can apply in your code..
Take 3 controls.where you can select the Category,SubCategory and Products
Now First Control feel with Category.
second control which is SubCategory should be filled by CategoryID,
thierd Control which is Product shold be filled by either categoryID or SubCategoryID
You need 3 Pages also,
1)Where you can enter category
2) where you can enter suncategory with or without CategoryID
3) Where you can enter product with or without subcategoryID

->> so, it's all dependent from the master table to child table.

    you will get one power structure.

->> Hope I am clear but still if you find any difficulty to understand this. please let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.