<html>
<head><title>EZ Shopper - Products and Stocks Menu
</title>
<script language="JavaScript">
function newWindow(mypage,myname,w,h,features) {
  if(screen.width){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  }else{winl = 0;wint =0;}
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}
</script>
</head>
<body>
<?php
/***************************************************************************
 *			prodstocks_add.php            	                   *
 *                     	          				           *
 *   File Info:	Add Products/Stocks Record  (default)                      *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/

include_once("./header.inc");
include_once("./config.php");
@session_start();

if ($_SESSION['admin'] != "admin" or $_SESSION['admin'] == NULL)
{
	 echo "<p align=\"center\"><b><strong><font face = Verdana size = 5>Sorry you not the administrator!</strong></font></b><br>Click here to return to log in screen <a href='shop_showall.php'>here.</a><br><br><br><br><br><br></p>";
	include("./your-footer.inc");  
	exit();
}
if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $choice = addslashes($_POST["choice"]);
        $pid = addslashes($_POST["pid"]);
        $pitem = addslashes($_POST["pitem"]);
        $pdes = addslashes($_POST["pdes"]);
        $price = addslashes($_POST["price"]);
        $scat = addslashes($_POST["scat"]);
        $color = addslashes($_POST["color"]);
        $size = addslashes($_POST["size"]);
        $price = addslashes($_POST["price"]);
        $stock = addslashes($_POST["stock"]);
        $spic = addslashes($_POST["spic"]);

}

########## handle adding ################

if ($choice == "add")
{
	echo "<center><img src=images/computer.gif border=0><font face=verdana size=3 color=navy>Adding a Record to PRODUCTS AND STOCKS</font></center>";
	if ($pid != NULL and $pitem!= NULL and $pdes != NULL and $price != NULL and $scat != NULL and $size != NULL and $color != NULL)
	{

	}
	else
	{
		echo("<br><img src=./images/line.gif border=0 height=1 width=100%><br><center>Cannot continue adding product/stocks record - all fields must contain data!<br>");
		echo("Go back and add details before continuing to add a new product/stocks record!<br>");
		echo "<br>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></b></center><img src=./images/line.gif border=0 height=1 width=100%><br><br>";
		include_once("./footer.inc");
		exit();	
	}
	if ($pitem != NULL) $sitem = $pitem;   // set $sitem = $pitem i.e stock item title same as product stock title.

	$addprod = "yes";
	$addstck = "yes";	// $addprod and $addstck flags for adding if yes can add if no cannot.....

	include_once("./config.php");

	################## connect to database ####################
	$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
	$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database ...Please press 'Create Database' to create new database.");
	########################################################

	$table = "products";
	$sql = "SELECT * from $table where pid = $pid";
	$result = @mysql_query($sql,$conn);
	$numprods = @mysql_num_rows($result);

	if ($numprods > 0)	$addprod = "no";
	
	$table = "stocks";
	$sqls = "SELECT * from $table where pid = $pid";
	$results = @mysql_query($sqls,$conn);
	$numstck = @mysql_num_rows($results);

	if ($numstck > 0) $addstck = "no";

	###################### display warning message if product/stock record exists ######################

	if ($addprod == "no" and $addstck == "no")
	{
		echo("<br><img src=./images/line.gif border=0 height=1 width=100%><br><center>Cannot continue adding product/stocks record - record already exists!<br>");
		echo("There already exists a product and stock record with stock/product ID = $pid<br>");
		echo "<br>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></b></center><img src=./images/line.gif border=0 height=1 width=100%><br><br>";
		include_once("./footer.inc");
	}

	################### do some validation ########################
	$bads = "false";
	if (($addprod == "yes" and $addstck == "yes") or ($addprod == "no" and $addstck == "yes") or ($addprod == "yes" and $addstck == "no"))
	{
		$baderr = isBadNum($pid);
		if ($baderr) 
		{
		$line1 = "Cannot continue adding the record - Product/Stock ID fieldmust contain characters '0-9' <br>";
		$bads = "TRUE";
		}
		$baderr1 = isBadNum($price);
		if ($baderr1) 
		{
		$line2 = "Cannot continue adding the record - Price field must contain characters '0-9' and '.' <br>";
		$bads= "TRUE";
		}
		$baderr2 = isBadNum($stock);
		if ($baderr2) 
		{
		$line3 = "Cannot continue adding the record - Stock field must contain characters '0-9' <br>";
		$bads = "TRUE";
		}
	}
	if ($bads == "TRUE")
	{
	echo "<center><table width=550 border=1 cellspacing=0 cellpadding=0 height=80>
<tr><td width=550 bgcolor=#CCCCCC height=10>$header</td></tr>
<tr><td width=550 bgcolor=#FFFFFF height=70>";
	echo($line1);
	echo($line2); 
	echo($line3); 
	echo("Go back and add details before continuing to add product/stock record!!<br><br></tr></table></center>");
	include("./footer.inc");
	exit();	
	}

	##### do the adding part #####
	
	if ($addprod == "yes" and $addstck == "yes")
	{
		$table = "products";
		$sql = "INSERT INTO $table (pid, pitem,pdes,price) VALUES ('$pid','$pitem','$pdes','$price')";
		$sql_result = mysql_query($sql,$conn)
	or die("Couldn't add record.");
		if (!$sql_result) 
		{     
			echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><center>Couldn't add the product record - please try again!<br>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></center><img src=./images/line.gif border=0 height=1 width=100%><br><br>";
			include_once("./footer.inc");
			exit();
		} else 
		{
			echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><b>Product ID: </b>$pid<br><b>Product Item: </b>$pitem<br><b>Product Description: </b>$pdes<br><b>Price: </b>$price<br><br><img src=.\images\window.gif border=0>Successfully added product record to database.......<img src=./images/line.gif border=0 height=1 width=100%><br>";
		}
		$table = "stocks";
		$sqls = "INSERT INTO $table (pid, sitem, scat, spic,stock,color,size)
	VALUES ('$pid', '$sitem', '$scat', '$spic','$stock','$color','$size')";
		$sql_results = mysql_query($sqls,$conn)
	or die("Couldn't add record.");
		if (!$sql_results) 
		{     
			echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><center>Couldn't add the stocks record - please try again!<br>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></center><img src=./images/line.gif border=0 height=1 width=100%><br><br>";
			include_once("./footer.inc");
			exit();
		} else 
		{
			echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><b>Stock ID: </b>$pid<br><b>Stock Item: </b>$sitem<br><b>Category: </b>$scat<br><b>Picture: </b>$spic<br><b>Stock: </b>$stock<br><b>Color: </b>$color<br><b>Size: </b>$size<br><br><img src=.\images\window.gif border=0>Successfully added product record to database.......<img src=./images/line.gif border=0 height=1 width=100%><br>";
		}
		echo "<br><center>Return back to <b><a href=./prodstocks.php>products/stocks menu</a><br>Return back to <b><a href=./prodstocks_adds.php>add products/stocks menu</a></center>";
	}

	if ($addprod == "yes" and $addstck == "no")
	{
		$table = "products";
		$sql = "INSERT INTO $table (pid, pitem,pdes,price) VALUES ('$pid','$pitem','$pdes','$price')";
		$sql_result = mysql_query($sql,$conn)
	or die("Couldn't add record.");
		if (!$sql_result) 
		{     
			echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><center>Couldn't add the product record - please try again!<br>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></center><img src=./images/line.gif border=0 height=1 width=100%><br><br>";
			include_once("./footer.inc");
			exit();
		} else 
		{
			echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><b>Product ID: </b>$pid<br><b>Product Item: </b>$pitem<br><b>Product Description: </b>$pdes<br><b>Price: </b>$price<br><br><img src=.\images\window.gif border=0>Successfully added product record to database.......<img src=./images/line.gif border=0 height=1 width=100%><br>";
		}
		echo "<br><img src=./images/line.gif border=0 height=1 width=100%><img src=.\images\window.gif border=0><br>Unable to add stocks record - possibly stock record already exists!!!><img src=./images/line.gif border=0 height=1 width=100%><br>";
		echo "<br><center>Return back to <b><a href=./prodstocks.php>products/stocks menu</a><br>Return back to <b><a href=./prodstocks_adds.php>add products/stocks menu</a></center>";
	}
	if ($addprod == "no" and $addstck == "yes")
	{
		echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><img src=.\images\window.gif border=0>Unable to add product record - possibly product record already exists!!!><img src=./images/line.gif border=0 height=1 width=100%><br>";
		$table = "stocks";
		$sqls = "INSERT INTO $table (pid, sitem, scat, spic,stock,color,size)
	VALUES ('$pid', '$sitem', '$scat', '$spic','$stock','$color','$size')";
		$sql_results = mysql_query($sqls,$conn)
	or die("Couldn't add record.");
		if (!$sql_results) 
		{     
			echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><center>Couldn't add the stocks record - please try again!<br>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></center><img src=./images/line.gif border=0 height=1 width=100%><br><br>";
			include_once("./footer.inc");
			exit();
		} else 
		{
			echo "<br><img src=./images/line.gif border=0 height=1 width=100%><br><b>Stock ID: </b>$pid<br><b>Stock Item: </b>$sitem<br><b>Category: </b>$scat<br><b>Picture: </b>$spic<br><b>Stock: </b>$stock<br><b>Color: </b>$color<br><b>Size: </b>$size<br><img src=.\images\window.gif border=0>Successfully added stock record to database.......<img src=./images/line.gif border=0 height=1 width=100%><br>";
		}
		echo "<br><center>Return back to <b><a href=./prodstocks.php>products/stocks menu</a><br>Return back to <b><a href=./prodstocks_adds.php>add products/stocks menu</a></center>";
	}
}
	
########## display the textboxes ############

if ($choice == NULL)
{
	include("./config.php");
	$table = "category";
	$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
	$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database...Please press 'Create Database' to create new database.");
	$sql = " SELECT * from $table order by category";
	$rs = mysql_query($sql,$conn)
	or die("Could not execute SQL query!!! Possibly SQL query already been executed.");
	$number = mysql_num_rows($rs);
	$form = "<form method=post action=prodstocks_adds.php>";
	$form.= "<table align=center width=500 cellspacing=0 border=1>";
	$form.="<tr><td colspan=2 bgcolor=#ccddff><b><font face=verdana size=2>Add New Product and New Stock Record:</font><b></td></tr>";
	$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Product / Stock ID </font></b></td><td width = 350><input type=text class=textbox name=pid size=7 maxlength=10><img src=./images/search.gif border=0><a href= javascript:newWindow('./checkid.php','acePopup',450,350,'scrollbars=yes')>Check Product ID</a></td></tr>";
	$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Product/Stock Item </font></b></td><td width = 350><input type=text class=textbox name=pitem size=30 maxlength=50></td></tr>";
	$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Product Description </font></b></td><td width = 350>
        <textarea rows=3 name=pdes cols=40></textarea></td></tr>";
	$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Price </b></td><td width = 350><input type=text class=textbox name=price size=6 maxlength=10></td></tr>";
	$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Category</font> </b></td><td width =350><select name = scat>";
	if ($number ==0)
	{
		$form.="<option value = \"No Category\">-- No Category --</option>";
	}
	else
	{
		while ($row = mysql_fetch_array($rs))
		{
			$category = $row["category"];
			$form.="<option value = \"$category\">-- $category --</option>";
		}
	}
	$form.="</select></td></tr>";
	$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Picture </font></b></td><td width = 350><input type=text class=textbox name=spic size=30 maxlength=50><a href = javascript:newWindow('./upload.html','acePopup',450,600,'scrollbars=yes')><font face=verdana size=2>Upload Picture</font></a></td></tr>";
	$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Stock </font></b></td><td width = 350><input type=text class=textbox name=stock size=6 maxlength=10></td></tr>";
	$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Color </font></b></td><td width = 350><input type=text class=textbox name=color size=25 maxlength=35></td></tr>";
$form.="<tr><td width=150><font color = #DD0000>*</font><b><font face=verdana size=2>Size</font> </b></td><td width = 350><input type=text class=textbox name=size size=25 maxlength=35></td></tr>";
	$form.="<tr><td width=150>&nbsp;</td><td width=350><input type=submit value='Add Record'><input type = reset value = 'Clear'></td></tr>";
	$form.="<input type = hidden name=choice value='add'>";
	$form.="</table></form>";
	echo $form;
	
echo "<img src=images/line.gif border=0 width=100%><br><br><center><img src=images/readme.gif border=0></center><br><table border=0 width=100%><tr><font face=verdana size =2 color=green>N.B Textboxes marked with <font color=#CC0000>*</font> must be filled in before product/stock record can be added.</tr><tr><td width=150><font face=verdana size =2 color=navy>Product/Stock ID</td><td><font face=verdana size =2>this is a numerical value.</font></td><tr><td><font face=verdana size =2 color=navy>Product/Stock Item</td><td><font face=verdana size =2>this is the title for the product and stock record.</font>
</td></tr><tr><td><font face=verdana size =2 color=navy>Product Description</td><td><font face=verdana size =2>this is a description for the product item.</font></td></tr><tr><td><font face=verdana size =2 color=navy>Price </td><td><font face=verdana size =2>this is numerical value representing the price of the product.</font></td></tr>
<tr><td><font face=verdana size =2 color=navy>Stock</td><td><font face=verdana size =2>this is numerical value representing stocks level for a product.</font></td></tr>
<tr><td><font face=verdana size =2 color=navy>Category </td><td><font face=verdana size =2>this represents the user-defined category of the product.</font></td></tr>

<tr><td><font face=verdana size =2 color=navy>Color</td><td align=justify><font face=verdana size =2>this is colour description for the stock record. <br>If you wish to omit type n/a in the textbox.</font></td></tr>
<tr><td><font face=verdana size =2 color=navy>Size</td><td align=justify><font face=verdana size =2>this is the size description for the stock record. <br>If you wish to omit type n/a in the textbox.</font></td></tr></font></table>
<center><br><font face=verdana size=2 color=navy>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></font></center></b>
<br><br><p align = justify><font face = verdana size = 2 color = 0000CC> NOTE TO USERS: Also if you input the location for pictures, the location is ./images/xxxx.jpg where xxxx = name of picture.If you uploading pictures for the enlarged image - please make sure you use the format xxxx-big.jpg or xxxx-big.gif or xxxx-big.png where xxxx is filename.</font></p>
<br><p align = justify><font face = verdana size = 2 color = navy> NOTE TO USERS: After you uploaded the picture using the Upload picture function, please input the location for the picture. The format is ./images/xxxx.jpg or ./images/xxxx.gif where xxxx = name of picture. You must add the location of the picture else the product picture cannot be displayed properly</font></p>
<img src=images/line.gif width=100%>";
}
include_once("./footer.inc");

######### functions ###########
 function isBadNum($str)
 {
   $x = 0;
  if (strlen($str)==0) return true;
 if(strlen($str)>=1&&strlen($str)<=50){
      $valid_chars = "0123456789.";      
	for($i=0;$i<strlen($str);$i++){
        if(strchr($valid_chars,substr($str,$i,1))==false) $x++;
      }
      if($x>0) return true;
    }else{
      return true;
    }
}
function isBadStrs($str)
{
    if(strlen($str)>=1&&strlen($str)<=150){
    $valid_chars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.'-()0123456789,.#";
      for($i=0;$i<strlen($str);$i++){
        if(strchr($valid_chars,substr($str,$i,1))==false) $x++;
      }
      if($x>0) return true;
    }else{
      return true;
    }
}
?>
</body>
</html>