Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

how to slove this category and subcategory code in php mysql

$
0
0

Hi friends i used this php for category and subcategory but this code is not proper work . So Friends i need your help Please tell my error or suggest me another script who has proper work  . its urgent guys .

 

 

 

 

index.php

 

 

<?php
$connect = mysql_connect($dbhost, $dbusername, $dbpassword) or
        die("Couldn't connect to DB");
$selectdb = mysql_select_db($dbname) or
        die(mysql_error());
?>

<html>
<head>

<script type="text/javascript">
function AjaxFunction(cat_id)
{
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
          try
                        {
                    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                    }
              catch (e)
                    {
                try
              {
              httpxml=new ActiveXObject("Microsoft.XMLHTTP");
              }
                catch (e)
              {
              alert("Your browser does not support AJAX!");
              return false;
              }
            }
  }
function stateck()
    {
    if(httpxml.readyState==4)
      {
var myarray=eval(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for(j=document.testform.subcat.options.length-1;j>=0;j--)
{
document.testform.subcat.remove(j);
}
for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray[i];
optn.value = myarray[i];
document.testform.subcat.options.add(optn);
}
      }
    }
    var url="dd.php";
url=url+"?cat_id="+cat_id;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
  }
</script>
</head>
<body>
<table width="100" border="0" align="center">
<form name="testform" method='POST' action=''>

<input type="hidden" name="id" value=""/>

<tr>
    <td colspan="2" align="center"><h3>Add Product</h3></td>
  </tr>
 
  <tr>
    <td>Company Name:</td>
    <td><input type ="text" name ="name" id="name"  size ="80" /></td>
  </tr>
  <tr>
    <td>Image:</td>
    <td><input type ="file" name ="image"  id="image" size ="80" /></td>
  </tr>
  <tr>
    <td> Date Of Birth:</td>
    <td>
    <select name="year" id="year"><option value="00">Year</option>
<?php
for($i=1900;$i<2020;$i++)
{
    echo"<option value=$i> $i </option>";
}    
?>
</select>
    
<select name="month" id="month"><option value="00">Month</option>
<?php
for($i=1;$i<=12;$i++)
{
    echo"<option value=$i> $i </option>";
}    
?>
</select>
    
<select name="day" id="day"><option value="00">Day</option>
<?php
for($i=1;$i<32;$i++)
{
    echo"<option value=$i> $i </option>";
}    
?>
</select></td>
  </tr>
 
  <tr>
    <td valign="top">Full Info:</td>
    <td><textarea name ="bio" id="bio"  rows="15" cols = "60" ></textarea></td>
  </tr>
 
  <tr>
    <td valign="top">Ph No:</td>
    <td><input type ="text"  name ="ph" id="ph" size ="80" /></td>
  </tr>
    <tr>
    <td valign="top">States:</td>
    <td><input type ="text"   name ="states"  id="states"  size ="80" /></td>
  </tr>
 
<tr>
    <td valign="top">Near By:</td>
    <td><input type ="text"  name ="cities" id="cities"  size ="80" /></td>
  </tr>            
    <tr>
    <td valign="top">More Add Keywords:</td>
    <td><input type ="text"  name ="keyword" id="keyword"  size ="80" /></td>
  </tr>    
  <tr><td>Select Category</td>
<td> <select name=cat onchange="AjaxFunction(this.value);">
<option value=''>Select One</option>
<?
require "db.php";// connection to database
$q=mysql_query("select * from category");
while($n=mysql_fetch_array($q)){
echo "<option value=$n[cat_id]>$n[category]</option>";
}
?>
</select></td></tr>
<tr><td>Sub Category</td><td>
<select name=subcat>
</select>
</td>
    </tr>
    <tr>
    <td colspan="2" align="center"><input type ="submit" name = "submit" value ="Submit"  /> <input type ="reset" value ="Reset" /></td>
  </tr>
</form>
</div>
   <!-- end of right content -->   
 </div><!-- end of main content -->
 
</div>
</body>
</html>
<?php

if(isset($_POST['submit']))
{
$bio = mysql_real_escape_string($_POST['name']);
$dob = $_POST['year']."/".$_POST['month']."/".$_POST['day'];
$bio = mysql_real_escape_string($_POST['bio']);
$ph =mysql_real_escape_string($_POST['ph']);
$keyword =mysql_real_escape_string($_POST['keyword']);
$states =mysql_real_escape_string($_POST['states']);
$cities =mysql_real_escape_string($_POST['cities']);
$cat = mysql_real_escape_string($_POST['cat']);
$subcat = mysql_real_escape_string($_POST['subcat']);
$image = $_FILES['image']['name'];
$image0 = $_FILES['image']['name'];
$gender=mysql_real_escape_string(($_REQUEST['gender']));
$datewa=date("d_m_Y_G_i_s");
$image=$datewa.$image0;
$dest = "stuff/".$image;
echo $image;
$samples=$_FILES['image']['tmp_name'];
if (move_uploaded_file($samples,$dest))
{
$sql = "INSERT into product(name,dob,bio,ph,keyword,states,cities,cat,subcat,image,gender) VALUES ('$name','$dob','$bio','$ph','$keyword','$states','$cities','$cat','$subcat','$image','$gender')";
mysql_query($sql);
echo "POST HAS BEEN ADDED";
}
else
{
echo "error"  .mysql_error();
}
}
?>
 

 

subcategory.php

 

<?
$cat_id=$_GET['cat_id'];
require "db.php";
$q=mysql_query("select * from subcategory where cat_id='$cat_id'");
echo mysql_error();
$myarray=array();
$str="";
while($nt=mysql_fetch_array($q)){
$str=$str . "\"$nt[name]\"".",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
echo "new Array($str)";

?>

 

 

add category.php

 

<form method= "post" name = "category" action="" enctype="multipart/form-data">
 <tr>
 <td colspan="2" align="center">Add Category</td>
</tr>
<tr>
<td>Name:</td>
 <td><input name="category" type="text" value="" size="80"  /></td>
  </tr>
 
 
  <tr>
    <td colspan="2" align="center"><input type = "submit" name = "submit" value = "Submit" />
        <input type = "reset" name = "reset" value = "Reset" /></td>
  </tr>
  </form>
</table>
<?php

if(isset($_POST['submit']))
{

$category = mysql_real_escape_string($_POST['category']);

$sql = "INSERT into category(category) VALUES ('$category')";
mysql_query($sql);
echo "POST HAS BEEN ADDED ";
}
else
{
echo  mysql_error();
}

?>

 

 

add subcategory

 

<form method= "post" name = "category" action="" enctype="multipart/form-data">
 <tr>
 <td colspan="2" align="center">Add Sub Category</td>
</tr>
<tr>
<td>Subcategory</td>
 <td><input name="subcat" type="text" value="" size="80"  /></td>
  </tr>
  category Name:
    
<select name="cid">
<?php
$selectcategory=mysql_query("select distinct(category),cat_id from category ");
while($r=mysql_fetch_array($selectcategory))
{            
    echo "<option value='".$r['cat_id']."'>".$r['category']."</option>";

}

    ?>
</select>

  <tr>
    <td colspan="2" align="center"><input type = "submit" name = "submit" value = "Submit" />
        <input type = "reset" name = "reset" value = "Reset" /></td>
  </tr>
  </form>
</table>
<?php
if(isset($_POST['submit']))
{
$subcat = mysql_real_escape_string($_POST['subcat']);
$cat_id = mysql_real_escape_string($_POST['cid']);
$ghj=mysql_query("Select * from category where id='$cat_id' ");
while($tyu=mysql_fetch_array($ghj))
{
$name=$tyu['name'];
}
$name = mysql_real_escape_string($_POST['name']);
$sql = "INSERT INTO subcategory(cat_id,name ) VALUES ('$cat_id','$subcat')";
mysql_query($sql);
echo "POST HAS BEEN ADDED ";
}
?>


Viewing all articles
Browse latest Browse all 13200

Trending Articles