<?php
require('connection1.php');
echo"<h1>GusetBook</h1>";
//if the name of this page  is change,we will this function to be easy
$firstpage=$_server['PHP_SELF'];
$id=$_POST['id'];
$name = $_POST['name'];
 $email = $_POST['email'];
 $message = $_POST['message'];
 $date=date('Y-m-d');
 $time=date('H:i:s');
 $submit = $_POST['submit'];
 $reset = $_POST['reset'];

  if($submit){
    //validation for the name field
    if($name=""){
     echo"please enter your name";
        }
        elseif(strlen($name)>0 and strlen($name)<7 ){
          echo"at least 7 characters";
        }
        elseif(is_numeric($name)){
          echo"please don't enter numbers";
        }
        else{

        }
    //validation for email field
    if($email=""){
      echo"please enter the email";
    }
   $len=strlen($email);
   while($i<$len){
     $x=substr($email,$i,1);
     if($x!='@'){
       $email="incorrect e-mail";
     }
     $i++;
   }
    //validation for passward field
    if($message=""){
      echo"please enter your message";
    }
    elseif($message!=strip_tags($message)){
                echo strip_tags($message);
    }
    else{
      echo nl2br($message);
    }
    echo $name - "<br>" - $email-"<br>" - $message;
    //storing data
    $data="insert into users(id,name,email,message,date,time) values('','$name','$email','$message','$date','$time')";
     $qury=mysql_query($data);
     if(!$qury){
       echo mysql_error();
     }

  }

 //to make horizontial line to write html code
  echo"<hr>";

   //showing data

  $data1="select * from users";
  $query1=mysql_query($data1);
if(!$query1){
     echo mysql_error();
      }


//to make refresh
echo"please wait for seconds....<meta http_equiv='refresh' content='5'>";
//show color of the table
if($id%2==0){
  $color='#69F33AD';
}
else{
  $color='#FFFFFF';
}
echo"<table bgcolor='$color'>";
 if($query1){
  while($getdata=mysql_fetch_assoc($query1) ){
     echo"$getdata[0].<br/>";
     echo"$getdata[1].<br/>";
     echo"$getdata[2].<br/>";
     echo"$getdata[3].<br/>";
     echo"$getdata[4].<br/>";
     echo"$getdata[5].<br/>";
   } }
   echo"<hr>";

echo"
<form action='$firstpage' method='POST'>
<table>
<tr><td>Your Name:</td>
<td><input name='name' type='text'> </td>
</tr>
<tr><td>Your Email:</td>
<td><input name='email' type='text'> </td>
</tr>
<tr><td valign='top'>Message:</td>
<td><textarea name='message' cols='30' rows='4' maxlength='500'></textarea>
</td></tr>
<tr>
<td><input type='submit' value='POST' name='submit'></td>
<td><input type='reset' value='Clear' name='reset'></td>
</tr>
</table>
</form>";
?>