<?php 
$con = mysql_connect('localhost','silver_tresor','admin.123');
if (!$con){
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("silver_tresorjewellery", $con);

$gemname = mysql_query('SELECT DISTINCT g.gemname,g.gemstone_id,pg.gem,pc.product_id,pc.category_id FROM gemstone g, product_gemavail pg, product_to_category pc WHERE pg.gemstone_id = g.gemstone_id AND pg.product_id = pc.product_id and pg.gem=1');
//$gemname = mysql_query('SELECT DISTINCT gemname FROM `gemstone`');
while($gname = mysql_fetch_array($gemname)) {
	$catname = mysql_query("SELECT DISTINCT name,category_id FROM `category_description` where name like '". $gname["gemname"] ."'");
	$GID = $gname["gemstone_id"];
	while($cname = mysql_fetch_array($catname)) {
	//echo $cname["name"].'<br>'.$gname["product_id"].'<br>'.$gname["gemstone_id"].'<br>';
	//mysql_query("UPDATE `product_to_category` SET `category_id`=concat(`category_id`, '$GID') where `product_id` = '". $gname["product_id"] ."'");
	$return = mysql_query("SELECT * FROM `product_to_category` Where `category_id`='$GID' and `product_id` = '". $gname["product_id"] ."'");
	//echo $return["category_id"].("SELECT * FROM `product_to_category` Where `category_id`='$GID' and `product_id` = '". $gname["product_id"] ."'").'<br>';
	//echo ("INSERT INTO `product_to_category` SET `category_id`='".$gname['gemstone_id']."',`product_id`='".$gname['product_id']."'").'<br>';
	if(!$return["category_id"])
	{
		//mysql_query("INSERT INTO `product_to_category` SET `category_id`='$GID' where `product_id` = '". $gname["product_id"] ."'");
		//mysql_query("INSERT INTO `product_to_category`(`category_id`,`product_id`) VALUES ($GID,$gname['product_id']) WHERE `product_id` = '". $gname["product_id"] ."'");
		//mysql_query("INSERT INTO `product_to_category`(category_id,product_id) VALUES ('$GID','$gname['product_id']') WHERE product_id = '. $gname['product_id'] .'");
		mysql_query("INSERT IGNORE INTO `product_to_category` SET `category_id`='".$cname['category_id']."',`product_id`='".$gname['product_id']."'");
		//mysql_query("DELETE FROM `product_to_category` Where `category_id`='".$cname['category_id']."'");
		}
	}
}

$catname = mysql_query("SELECT DISTINCT b.name,a.category_id,c.metal_id FROM category a , `category_description` b,metal c where a.category_id=b.category_id and a.parent_id= 103 and b.name =c.metal_code");

while($mname = mysql_fetch_array($catname)) { 
	$GID = $mname["category_id"];
	$pname = mysql_query("SELECT distinct product_id FROM `product_metalavail` Where `metal_id` = '". $mname["metal_id"] ."' and metal=1");
	while($prdname = mysql_fetch_array($pname)) 
	{
		if($prdname["product_id"])
		{
			mysql_query("INSERT IGNORE INTO `product_to_category` SET `category_id`='".$mname['category_id']."',`product_id`='".$prdname['product_id']."'");
			echo "INSERT IGNORE INTO `product_to_category` SET `category_id`='".$mname['category_id']."',`product_id`='".$prdname['product_id']."'" ;
		}
	}
}


if(!$gemname)
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($con);
?>