<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Add a Record</TITLE>
</HEAD>
<BODY>
<?
/***************************************************************************
 *			do_addrecord1.php	                           *
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~	                   *
 *   File Info:		  Add contacts                                     *
 *   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 CONTACTS</font></center><br>
<?
$table = "contacts";
  if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $cid = addslashes($_POST["cid"]);
        $ctitle = addslashes($_POST["ctitle"]);
        $cfirst = addslashes($_POST["cfirst"]);
        $csurname = addslashes($_POST["csurname"]);
        $cadd1 = addslashes($_POST["cadd1"]);
        $cadd2 = addslashes($_POST["cadd2"]);
        $ccity = addslashes($_POST["ccity"]);
        $ccounty = addslashes($_POST["ccounty"]);
        $ccode = addslashes($_POST["ccode"]);
        $cphone = addslashes($_POST["cphone"]);
        $cfax = addslashes($_POST["cfax"]);
        $cemail = addslashes($_POST["cemail"]);
}
if ($cid==NULL and $ctitle == NULL and $cfirst == NULL and $csurname == NULL and $cadd1 == NULL and $cadd2 == NULL and $ccity == NULL and $ccounty==NULL and $ccode == NULL and $cphone==NULL and $cfax == NULL and $cemail==NULL)
{
echo("<center>Cannot continue adding contacts records - fields are all empty!</center>");
	echo("<center>Go back and add details before continuing to add a new contacts record!</center><br>");
	echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
	include_once("your-footer.inc");
	exit();
}

if ($cid!=NULL and $ctitle != NULL and $cfirst != NULL and $csurname != NULL and $cadd1 != NULL and $cadd2 != NULL and $ccity != NULL and $ccounty!=NULL and $ccode != NULL and $cphone!=NULL and $cemail!=NULL)
{
}else
{
	echo("<center>Cannot continue adding contacts records - some fields are empty!</center>");
	echo("<center>Go back and add details before continuing to add a new contacts record!</center><br>");
	echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
	include_once("your-footer.inc");
	exit();	
}
$baderr = isBadStr($cid);
if ($baderr) 
{
echo("<center>Cannot continue adding contact records - Contact ID must contain digits 0-9 & '.'</center>");
echo("<center>Go back and add details before continuing to add a new contacts record!</center><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr1 = isBadPhone($cphone);
if ($baderr1) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - Telephone must contain digits 0-9 & '.-()[]' and be >= 7 digits long</font></br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr2 = isBadEmail($cemail);
if ($baderr2) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records- email format should be xxx@yourdomain.com etc.</font><br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr3 = isBadStrs($ctitle);
if ($baderr3) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - Title should contain characters 'a-z A-Z'.</font><br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr4 = isBadStrs($cfirst);
if ($baderr4) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - First Name should contain characters 'a-z A-Z'.</font><br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr5 = isBadStrs($csurname);
if ($baderr5) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - Surname should contain characters 'a-z A-Z'.>/font><br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!<br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr6 = isBadStrs($cadd1);
if ($baderr6) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - Address #1 should contain characters 'a-z A-Z'.</font><br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr7 = isBadStrs($cadd2);
if ($baderr7) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - Address #2 should contain characters 'a-z A-Z'.</font><br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr8 = isBadStrs($ccity);
if ($baderr8) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - City should contain characters 'a-z A-Z'.</font><br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr9 = isBadStrs($ccounty);
if ($baderr9) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - County should contain characters 'a-z A-Z'.</font><br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
exit();	
}
$baderr10 = isBadPhone($cfax);
if ($baderr10) 
{
echo("<font face=verdana size=2>Cannot continue adding contacts records - Fax must contain digits 0-9 & '.-()[]' and be >= 7 digits long<br>");
echo("<font face=verdana size=2>Go back and add details before continuing to add a new contacts record!</font><br>");
echo "<center><a href=show_addrecord1.html><img src=images/back-to.gif border=0><font face=verdana size=2>  Back to Add Contact Record</font></a></center>";
include_once("your-footer.inc");
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 cid,ctitle,cfirst,csurname,cadd1,cadd2,ccity,ccounty,ccode,cphone,cfax,cemail from $table where cid = $cid";
$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>Contact ID:</font> ".$row["cid"]); 
       echo(" <br><font face=verdana size=2>Title: </font>".$row["ctitle"]);
       echo(" <br><font face=verdana size=2>First Name: </font>".$row["cfirst"]);
       echo(" <br><font face=verdana size=2>Surname: </font> ".$row["csurname"]."<br>");
       echo("<br><font face=verdana size=2>Address #1: </font>".$row["cadd1"]."<br>");
       echo("<br><font face=verdana size=2>Address #2: </font>".$row["cadd2"]."<br>");
       echo("<br><font face=verdana size=2>City: </font>".$row["ccity"]."<br>");
       echo("<br><font face=verdana size=2>County: </font>".$row["ccounty"]."<br>");
       echo("<br><font face=verdana size=2>Post Code: </font>".$row["ccode"]."<br>");
       echo("<br><font face=verdana size=2>Phone: </font>".$row["cphone"]."<br>");
       echo("<br><font face=verdana size=2>Fax: </font>".$row["cfax"]."<br>");
       echo("<br><font face=verdana size=2>Email: </font>".$row["cemail"]."<br>");
       echo("<br><font face=verdana size=2>Already exists in Contacts table!</font><br>");
       echo("<br><font face=verdana size=2>Press 'Back' button to return to previous menu. <br>");
 echo "<br><center><img src=images/line.gif border=0 width=400></center>";
       include_once("your-footer.inc");
       exit();
}
$sql = "INSERT INTO $table (cid, ctitle, cfirst,csurname,cadd1,cadd2,ccity,ccounty,ccode,cphone,cfax,cemail)
	VALUES ('$cid', '$ctitle', '$cfirst', '$csurname','$cadd1','$cadd2','$ccity','$ccounty','$ccode','$cphone','$cfax','$cemail')";
//echo($sql);
?>
<?
$sql_result = mysql_query($sql,$conn)
	or die("Couldn't add record.");

if (!$sql_result) {     
	echo "<P>Couldn't add record!";
} else {


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

<table border=0 width=500>
<tr>
<td valign=top><strong><font face=verdana size=2>Contact ID:</font></strong></td>
<td valign=top>$cid</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Title:</font></strong></td>
<td valign=top>$ctitle</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>First Name:</font></strong></td>
<td valign=top>$cfirst</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Surname:</font></strong></td>
<td valign=top>$csurname</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Address #1:</font></strong></td>
<td valign=top>$cadd1</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Address #2:</strong></td>
<td valign=top>$cadd2</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>City:</font></strong></td>
<td valign=top>$ccity</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>County:</font></strong></td>
<td valign=top>$ccounty</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Post Code:</font></strong></td>
<td valign=top>$ccode</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Phone:</font></strong></td>
<td valign=top>$cphone</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Fax:</font></strong></td>
<td valign=top>$cfax</td>
</tr>
<tr>
<td valign=top><strong><font face=verdana size=2>Email:</font></strong></td>
<td valign=top>$cemail</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;
    }
}
 function isBadPhone($str)
 {
   $x = 0;
  if (strlen($str)==0) {
    return false;
 }
 	if(strlen($str)>=7&&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 isBadEmail($str)
 {
	$atsign = 0;
	$dot = 0;
	$x = 0;
      if(strlen($str)>=4&&strlen($str)<=50){
      $valid_chars = " 0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.@";
      for($i=0;$i<strlen($str);$i++){
        if(strchr($valid_chars,substr($str,$i,1))==false) $x++;
      }
	 for($i=0;$i<strlen($str);$i++){
        if(strchr("@",substr($str,$i,1))==true) $atsign++;
      }
 	for($i=0;$i<strlen($str);$i++){
        if(strchr(".",substr($str,$i,1))==true) $dot++;
      }
       if ($atsign == 0) return true;
      if($x>0 and $atsign == 1 and $dot >= 1) 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;
    }
}
?>

<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>
