<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Add a Record</TITLE>
</HEAD>
<BODY>
<?
/***************************************************************************
 *			do_addrecord.php	                           *
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~	                   *
 *   File Info:		  Add Products Record                              *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/
include("./config.php");
include("./header.inc");
?>
<center><img src=images/computer.gif border=0><font face=verdana size=3 color=navy>  Adding a Record to PRODUCTS</font></center>
<br>
<?
$table = "products";
  if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $pid = addslashes($_POST["pid"]);
        $pitem = addslashes($_POST["pitem"]);
        $pdes = addslashes($_POST["pdes"]);
        $price = addslashes($_POST["price"]);
}
if ($pid==NULL and $pitem == NULL and $pdes == NULL and $price == NULL)
{
	echo("<center>Cannot continue adding products records - fields are all empty!</center>");
	echo("<center>Go back and add details before continuing to add a new products record!</center><br>");
	echo "<center><a href=show_addrecord.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Product Record</font></a></center>";
	include_once("your-footer.inc");
	exit();
}
if ($pid !=NULL and $pitem != NULL and $pdes != NULL and $price != NULL)
{

}else
{
	echo("<center>Cannot continue adding products records - all fields must contain data!</center>");
	echo("<center>Go back and add details before continuing to add a new products record!</center><br>");
	echo "<center><a href=show_addrecord.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Product Record</font></a></center>";
	include_once("your-footer.inc");
	exit();	
}
$baderr = isBadStr($price);
if ($baderr) 
{
echo("Cannot continue adding products records - Price must contain digits 0-9 & '.'<br>");
echo("Go back and add details before continuing to add a new products record!<br>");
exit();	
}
$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 pid,pitem,pdes,price from $table where pid = $pid";
$rs = @mysql_query($sql,$conn)
	or die("Could not execute SQL query!!! Possibly SQL query already been executed.");
while ($row = mysql_fetch_array($rs))
{
       echo "<center><img src=images/line.gif border=0 width=400></center><br><center>";
       echo("<font face=verdana size=2>Product ID: </font>".$row["pid"]); 
       echo(" <br><font face=verdana size=2>Product Item: </font>".$row["pitem"]);
       echo(" <br><font face=verdana size=2>Product Description: </font>".$row["pdes"]);
       echo(" <br><font face=verdana size=2>Price: $</font> ".$row["price"]."<br>");
       echo("<br><font face=verdana size=2 color=darkgreen>Already exists in Products table!</font><br>");
       echo("<br><a href=show_addrecord.html><img src=images/back-to.gif border=0><font face=verdana size=2> Back to Add Product Record</font></a><br></center>");
       echo "<br><center><img src=images/line.gif border=0 width=400></center>";
       include_once("your-footer.inc");
	exit();
}
$sql = "INSERT INTO $table (pid, pitem, pdes, price)
	VALUES ('$pid', '$pitem', '$pdes', '$price')";
//echo($sql);
?>
<?
$sql_result = mysql_query($sql,$conn)
	or die("Couldn't add record.");

if (!$sql_result) {     
	echo "<font face=verdana size=2 color=red>Couldn't add record!</font>";
} else {

echo "<p align=center><font face=verdana size=2>Product Record sucessfully added!</font></p>

<table border =0 width=400>
<tr>
<td valign=top><strong><font face=verdana size=2>Item ID:</strong></td>
<td valign=top>$pid</font></td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Item Title:</strong></td>
<td valign=top>$pitem</font></td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Item Description:</strong></td>
<td valign=top>$pdes</font></td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Item Price:</strong></td>
<td valign=top>$currency $price</font></td>
</tr>
</table>
"; 

}
function isBadStr($str)
{
    if(strlen($str)>=1&&strlen($str)<=150){
      $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;
    }
}
?>
<br>
<center><table width="500" border=1><tr align="justify" class="subtitle"><td>
  <p align="center">
  <img border="0" src="images/database.GIF" width="24" height="24"><font face=Verdana size=1><a href="./create_db.php">Create database</a>&nbsp;
  <img border="0" src="images/database.GIF" width="24" height="24"></font><a href="./delete_db.php"><font face=Verdana size=1>Delete database</font></a><font face="Verdana" size="1">&nbsp;
  <img border="0" src="images/Records.GIF" width="24" height="24"><a href="./create_menu.php">Create table</a>&nbsp;&nbsp;&nbsp;&nbsp;
  <img border="0" src="images/Records.GIF" width="24" height="24">&nbsp;
  </font> <a href="./add_menu.php"><font face=Verdana size=1>Add Record </font></a>  
  </p>
  <p align="center">&nbsp;<font face="Verdana" size="1"><img border="0" src="images/Records.GIF" width="24" height="24"></font><a href="./view_menu.php"><font face=verdana size=1>View Records</font></a>&nbsp;
  <font face="Verdana" size="1">&nbsp;&nbsp;&nbsp;
  <img border="0" src="images/Records.GIF" width="24" height="24"></font><a href="./del_menu.php"><font face=verdana size=1>Delete Record</font></a>
  <font face="Verdana" size="1">&nbsp;&nbsp;&nbsp;
  <img border="0" src="images/Records.GIF" width="24" height="24"></font> <a href="./upd_menu.php"><font face=verdana size=1>Update Record</font></a>&nbsp;
  <img border="0" src="images/home-to.GIF" width="24" height="24"><font face=verdana size=1>&nbsp; <a href="./index.php">Home</a></font>  </a></td></tr></table><br></center>
<img src=images/line.gif border=0 width=100%><br><br>
<?php include("./your-footer.inc");?>
</BODY>
</HTML>
