Category and subcategory in php

connection.php

<?php
/*host:localhost
user:root
password:
database: arvind
*/
error_reporting(E_ALL ^ E_NOTICE);
$conn=mysql_connect('localhost','root','');
if(!$conn)
{
echo "Connection Unsuccessfull"; exit;
}

mysql_select_db('arvind');
?>

<?php
include("connection.php");
if($_GET['id']!='')
{
$id=$_GET['id'];
	$sqlp="select * from product where id='$id'";
	$resp=mysql_query($sqlp);
	$rowp=mysql_fetch_assoc($resp);
}
?>

product.php

<html>
<head>
<script src="http://www.jquery4u.com/function-demos/js/jquery-1.6.4.min.js"></script>
<script>
function showsubcat(str)
{
$.ajax({
	url: 'ajax.php',
	type: 'POST',
	data: 'cat='+str,
	dataType: 'html',
	success: function(data, textStatus, xhr) {
	//alert(data);
			$('#subcategory').html(data);
	},
	error: function(xhr, textStatus, errorThrown) {
		$('#subcategory').html(textStatus);
	}
});
}
</script>
</head>
<body>
<form action="submit.php" method="post" name="frm" enctype="multipart/form-data">
<table width="100%" border="2" cellspacing="0" cellpadding="0">
  <tr>
    <td>Category</td>
    <td>
    <?php
    $sql="select * from category";
	$res=mysql_query($sql);
	?>
    <select name="category" onchange="showsubcat(this.value);">
    <option value="">Select Category</option>
    <?php
    while($row=mysql_fetch_assoc($res))
	{
	?>
    <option value="<?php echo $row['id'];?>" <?php if($row['id']==$rowp['cat_id']){ echo "selected";}?>><?php echo $row['name'];?></option>
    <?php
	}
	?>
    </select></td>
  </tr>
  <tr>
    <td>Sub Category</td>
    <td><?php
    $sql1="select * from subcategory where cat_id='$rowp[cat_id]'";
	$res1=mysql_query($sql1);
	?>
    <select name="subcategory" id="subcategory">
    <option value="">Select Sub Category</option>
    <?php
    while($row1=mysql_fetch_assoc($res1))
	{
	?>
    <option value="<?php echo $row1['id'];?>" <?php if($row['id']==$rowp['sub_id']){ echo "selected";}?>><?php echo $row1['name'];?></option>
    <?php
	}
	?>
    </select></td>
  </tr>
  <tr>
    <td>Product name</td>
    <td><input type="text" name="productname" value="<?php echo $rowp['product_name'];?>"  /> </td>
  </tr>
  <tr>
    <td>Qentity</td>
    <td><input type="text" name="quantity"  value="<?php echo $rowp['quantity'];?>" /> </td>
  </tr>
  <tr>
    <td>Price</td>
     <td><input type="text" name="price" value="<?php echo $rowp['price'];?>"  /> </td>
  </tr>
  <tr>
    <td>image</td>
     <td><input type="file" name="file"  /> </td>
  </tr>
  <tr>
     <td><input type="submit" name="submit" value="submit"  /> </td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

submit.php

<?php
include("connection.php");
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ($_FILES["file"]["name"]!='')
{
    if (file_exists("upload/" . $_FILES["file"]["name"]))
	{
	  $image=$_FILES["file"]["name"];
    }
	else
	{
      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
	 $image=$_FILES["file"]["name"];
    }
}
else
{
  echo $image='';
}
$sql="insert into product set cat_id='$_POST[category]', sub_id='$_POST[subcategory]',product_name='$_POST[productname]',price='$_POST[price]',quantity='$_POST[quantity]',image='$image'";
//echo $sql; exit;
mysql_query($sql);
header("Location:list.php");
//print_r($_POST);
//print_r($_FILES);
?>

list.php


<?php
include("connection.php");
if($_GET['id'])
{
	$id=$_GET['id'];
	mysql_query("delete from product where id='$id'");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="100%" border="2" cellspacing="0" cellpadding="0">
  <tr>
    <td>SNo</td>
    <td>Productname</td>
    <td>Price</td>
    <td>quantity</td>
    <td>image</td>
    <td>category;</td>
    <td>subcatagory</td>
    <td>Edit</td>
    <td>Delete</td>
  </tr>
  <?php
  $sql="select * from product";
  $res=mysql_query($sql);
  $sno++;
  while($row=mysql_fetch_assoc($res))
  {
  $cat_id=$row['cat_id'];
  $sql_cat="select * from category where id='$cat_id'";
  $res_cat=mysql_query($sql_cat);
  $row_cat=mysql_fetch_assoc($res_cat);

  $sub_id=$row['sub_id'];
  $sql_sub="select * from subcategory where id='$sub_id'";
  $res_sub=mysql_query($sql_sub);
  $row_sub=mysql_fetch_assoc($res_sub);
  ?>
  <tr>
    <td><?php echo $sno;?></td>
    <td><?php echo $row['product_name'];?></td>
    <td><?php echo $row['price'];?></td>
    <td><?php echo $row['quantity'];?></td>
    <td><?php echo $row['image'];?></td>
    <td><?php echo $row_cat['name'];?></td>
    <td><?php echo $row_sub['name'];?></td>
    <td><a href="product_edit.php?id=<?php echo $row['id'];?>">Edit</a></td>
    <td><a href="list.php?id=<?php echo $row['id'];?>">Delete</a></td>
  </tr>
  <?php
  $sno++;
  }
  ?>
</table>

</body>
</html>

product_edit.php

<?php
include("connection.php");
if($_GET['id']!='')
{
$id=$_GET['id'];
	$sqlp="select * from product where id='$id'";
	$resp=mysql_query($sqlp);
	$rowp=mysql_fetch_assoc($resp);
}
?>
<html>
<head>
<script src="http://www.jquery4u.com/function-demos/js/jquery-1.6.4.min.js"></script>
<script>
function showsubcat(str)
{
$.ajax({
	url: 'ajax.php',
	type: 'POST',
	data: 'cat='+str,
	dataType: 'html',
	success: function(data, textStatus, xhr) {
	//alert(data);
			$('#subcategory').html(data);
	},
	error: function(xhr, textStatus, errorThrown) {
		$('#subcategory').html(textStatus);
	}
});
}
</script>
</head>
<body>
<form action="update.php?id=<?php echo $id;?>" method="post" name="frm" enctype="multipart/form-data">
<table width="100%" border="2" cellspacing="0" cellpadding="0">
  <tr>
    <td>Category</td>
    <td>
    <?php
    $sql="select * from category";
	$res=mysql_query($sql);
	?>
    <select name="category" onchange="showsubcat(this.value);">
    <option value="">Select Category</option>
    <?php
    while($row=mysql_fetch_assoc($res))
	{
	?>
    <option value="<?php echo $row['id'];?>" <?php if($row['id']==$rowp['cat_id']){ echo "selected";}?>><?php echo $row['name'];?></option>
    <?php
	}
	?>
    </select></td>
  </tr>
  <tr>
    <td>Sub Category</td>
    <td><?php
    $sql1="select * from subcategory where cat_id='$rowp[cat_id]'";
	$res1=mysql_query($sql1);
	?>
    <select name="subcategory" id="subcategory">
    <option value="">Select Sub Category</option>
    <?php
    while($row1=mysql_fetch_assoc($res1))
	{
	?>
    <option value="<?php echo $row1['id'];?>" <?php if($row['id']==$rowp['sub_id']){ echo "selected";}?>><?php echo $row1['name'];?></option>
    <?php
	}
	?>
    </select></td>
  </tr>
  <tr>
    <td>Product name</td>
    <td><input type="text" name="productname" value="<?php echo $rowp['product_name'];?>"  /> </td>
  </tr>
  <tr>
    <td>Qentity</td>
    <td><input type="text" name="quantity"  value="<?php echo $rowp['quantity'];?>" /> </td>
  </tr>
  <tr>
    <td>Price</td>
     <td><input type="text" name="price" value="<?php echo $rowp['price'];?>"  /> </td>
  </tr>
  <tr>
    <td>image</td>
     <td><input type="file" name="file"  /> </td>
  </tr>
  <tr>
     <td><input type="submit" name="submit" value="submit"  /> </td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

update.php

<?php
include("connection.php");
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ($_FILES["file"]["name"]!='')
{
    if (file_exists("upload/" . $_FILES["file"]["name"]))
	{
	  $image=$_FILES["file"]["name"];
    }
	else
	{
      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
	 $image=$_FILES["file"]["name"];
    }
}
else
{
  echo $image='';
}
$id=$_GET['id'];
$sql="update product set cat_id='$_POST[category]', sub_id='$_POST[subcategory]',product_name='$_POST[productname]',price='$_POST[price]',quantity='$_POST[quantity]',image='$image' where id='$id'";
//echo $sql; exit;
mysql_query($sql);
header("Location:list.php");
//print_r($_POST);
//print_r($_FILES);
?>

ajax.php

 <?php
include("connection.php");
//echo $_POST['cat'];
?>
<?php
    $sql1="select * from subcategory where cat_id='$_POST[cat]'";
	$res1=mysql_query($sql1);
	?>
    <option value="">Select Sub Category</option>
	<?php
    while($row1=mysql_fetch_assoc($res1))
	{
	?>
    <option value="<?php echo $row1['id'];?>" <?php if($row['id']==$rowp['sub_id']){ echo "selected";}?>><?php echo $row1['name'];?></option>
    <?php
	}
	?>

Hi arvindkumar, welcome to the forums.

Looks like you were so intent on posting all that code you forgot to mention what problem you’re having with it :wink:

database-arvind
tablename-
category-id(auto increment),name
subcategory-id(auto increment),name,cat_id
product -id,cat_id,sub_id,product_name,quantity,price,image,type

and, the problem is… ? :slight_smile:

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.