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> </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> </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
}
?>