Saturday 9 January 2016

InsertUpdateDelete with Validtion

//Config.php
<?php

$cn = mysql_connect('localhost','root','') or die(mysql_error());
if(!$cn)
{
    echo "ERROR:not connected";
   
    }


$db = mysql_select_db('info') or die(mysql_error());
    if(!$db)
{
    echo "ERROR: Not Connected..";
   
    }   
?>


//Stud.php

<body>
<form action="" method="post" enctype="multipart/form-data">
  <div align="center">
    <fieldset>
      <legend>Stud-Info</legend>
       <div>
        <label>userId:-</label>
        <label>
            <input name="txtid" id="txtid" type="text" readonly="readonly"
             />
          </label>
      </div>
      <div>
        <label>Name:-<sup style="color:red">*</sup></label>
        <label>
            <input name="txtname" id="txtname" type="text" placeholder="Enater Name" />
          </label>
      </div>
     
      <br/>
      <div><input name="txtsignup" type="submit" value="Signup" onclick="return validate() " /></div>
    </fieldset>
  </div>
 
  <fieldset><legend>Griedview</legend>
  <table align="center" border="1"><caption><h3>Griedview</h3></caption>
   <tr>
  <th>userid</th>
  <th>Username</th>
  <th>Edit</th>
  <th>Delete</th>
  </tr>
  <?php
 
  $grid=mysql_query("Select * from Registration");
 
  //echo "<table><tr><th>Id</th> <th>name</th></tr>";
  
  while($gr=mysql_fetch_row($grid))
  {
    echo "<tr>";
    echo "<td>$gr[0]</td>";   
    echo "<td>$gr[1]</td>";
      echo "<td><a href=Stud.php?did=$gr[0]>Delete</a></td>";
    echo "<td><a href=update.php?uid=$gr[0]>Edit</a></td>";
  }
 
  ?>
 
  </table>

  </fieldset>
 
</form>

</body>


//validation on head
<script language="javascript" type="text/javascript">
function validate()

{
    var x1= document.getElementById("txtname");
        if(x1.value.length==0)
        {
            alert("please Enter UserName");
            x1.focus();
           
        return false;
           
            }
    }

</script>




 <?php
include_once("config.php");
extract($_POST);
if(isset($txtsignup))
{
    $res= mysql_query("insert into Registration (userName) Values('$txtname')") or
    die(mysql_error());
       
    }
   
        if(isset($_REQUEST['did']))
    {
        mysql_query("delete from Registration where userId=".$_REQUEST['did']);
    }
?>




//Update.php
 



<?php
     include_once("config.php");

    if(isset($_REQUEST['uid']))
   {
      $res=mysql_query("select * from registration where userId=".$_REQUEST['uid']) or die(mysql_error());
   
      $r=mysql_fetch_row($res);
   }
  
   if(isset($_POST['submit']))
   { 
 mysql_query("update registration set userName = '".$_POST['fname']."'
 where userId='".$_POST['id']."'")or die(mysql_error());
      header('location:Stud.php');
     
     
   }
?>


<br>

<form id="tform" name="frm1" action="" method="post" enctype="multipart/form-data">
<div></div>
<table align="center" border="2">
<h1 align="center">Edit form</h1>
<tr>
<td>id:</td>
<td><input type="text" name="id" id="id" value="<?php echo $r[0];?>" /></td>
</tr>
<tr>
<td>first name:<sup style="color:#C30">*</sup></td>
<td><input type="text" name="fname" id="fname" value="<?php echo $r[1];?>" /></td>
</tr>
<td>
<input type="submit" name="submit" id="submit" value="edit"/>
</td></tr>
</table>
</form>


Session

//Login.php
<?php
session_start();
   
   
    if(isset($_POST['s1']))
    {
    $x=$_POST['t1'];
    $_SESSION['user']=$x;
    $y=$_POST['t2'];
   

if(isset($_SESSION['user']))
            {
                header("Location:session1.php");   
            }   
    else
        {
            echo "Sorry........";
        }
    }

?>
<form id="form1" name="form1" method="post" action="">
  <table width="300" border="1">
    <tr>
      <td width="88">User</td>
      <td width="196"><input name="t1" type="text" id="t1" /></td>
    </tr>
    <tr>
      <td>Password</td>
      <td><input name="t2" type="text" id="t2" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <input name="s1" type="submit" id="s1" value="Submit" />
      </div>  <div align="center"></div></td>
    </tr>
  </table>
</form>


//Session.php


<?php
session_start();
if(isset($_SESSION['user']))
{
    echo "Hi " .$_SESSION['user'];
}
?>

Cookies



<?php
if(isset($_POST['s1']))
{
    $u=$_POST['t1'];
        if(isset($_POST['c1']))
        {
            setcookie('mydt',$u,time()+60*60*24*7);
            echo "Cookies stored.........";
            echo $u;
        }
}
?>

<?php
    if(isset($cookie['mydt']))
    {
    echo $u."Cookie Retrived";
    echo ($_COOKIE['mydt']);
    echo "cookies Retrived...";
    }

?>
<form id="form1" name="form1" method="post" action="">
  <table width="300" border="1" align="center">
    <tr>
      <td width="88">User</td>
      <td width="196"><input name="t1" type="text" id="t1" /></td>
    </tr>
    <tr>
      <td>Password</td>
      <td><input name="t2" type="text" id="t2" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
          <input name="c1" type="checkbox" id="c1" value="checkbox" />
      Remember Me </div></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <input name="s1" type="submit" id="s1" value="Submit" />
      </div>  <div align="center"></div></td>
    </tr>
  </table>
</form>