Tuesday, February 15, 2011

insert,delet,update oops

includes folder

dbconnect.php

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);


  $con = mysql_connect("localhost","root","") or die("fail to connect".mysql_error());
  mysql_select_db("base",$con) or die(mysql_error());
?>
main.php

<?
class operations{

  
   public $cid;
   public $cname;
   public $cfather;
   public $caddress;
  
   public function insert(){
     $sql = "INSERT INTO customer(custname,custfather,address)
              VALUES('".$this->cname."', '".$this->cfather."', '".$this->caddress."')";
     mysql_query($sql) or die("insert:".mysql_error());
   
   }
  
   //delete record
   public function delete(){
  
     $sql = "DELETE FROM customer WHERE custid=".$this->cid;
     mysql_query($sql) or die("delete".mysql_error());
    }
   
    //updating the records
   public function update(){
   
     $sql = "UPDATE customer SET custname = '".$this->cname."',
                                custfather = '".$this->cfather."',
                               address    = '".$this->caddress."'
           WHERE custid ='".$this->cid."'";
     mysql_query($sql) or die("update:".mysql_error());
    }
  
   // Display the records 
   public function display(){
     $temp_arr = array();
     $res = mysql_query("SELECT * FROM customer") or die(mysql_error());       
     $count=mysql_num_rows($res);
   
    while($row = mysql_fetch_array($res)) {
         $temp_arr[] =$row;
     }
     return $temp_arr; 
     }
     }
 ?>

.................................................
change.php

<?php
  require('includes/dbconnect.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
  <?php
    $cno=$_GET['cno'];
    $res=mysql_query("select * from customer where custid=$cno") or die(mysql_error()) ;
    $row=mysql_fetch_array($res);
  ?>
  <form action="index.php" method="post">
   <input type="hidden" name="cid" value="<?php echo $cno?>"  />
   <table><tr><td>Customer Name</td><td><input type='text' name='custname' value="<?php echo $row['custname']?>" /></td></tr>
        <tr><td>Father Name</td><td><input type='text' name='cfather' value="<?php echo $row['custfather'] ?>" /></td></tr>
        <tr><td>address</td><td><input type='text' name='caddress' value="<?php echo $row['address'] ?>" /></td></tr>
        <tr><td colspan='3'><input type='submit' name='update' value='submit' /></td></tr>
  </table>
  </form>       
 
</body>


index.php


<?php
 require_once('includes/dbconnect.php');
 require_once('includes/main.php');

 //object creation

  $obj = new operations();

 if(isset($_POST['submit'])){
     $obj->cname = $_POST['custname'];
     $obj->cfather = $_POST['cfather'];
     $obj->caddress  = $_POST['caddress'];   
     if($obj->cname=="" || $obj->cfather=="" || $obj->caddress==""){
        echo "please insert all the fields <br>";
     }
     //insert
     else{
   
     $obj->insert();
     header("Location:index.php");
     exit;
     }
  }
 
  if(isset($_POST['update'])){
   
    $obj->cid = $_POST["cid"];
    $obj->cname = $_POST['custname'];
    $obj->cfather = $_POST['cfather'];
    $obj->caddress  = $_POST['caddress'];
    if($obj->cname=="" || $obj->cfather=="" || $obj->caddress==""){
        echo "please insert all the fields";
     }
     else{
    $obj->update();
     header("Location:index.php");
     exit;
     }
  }
 
  if (isset($_GET["cno"])){
     
    $obj->cid = $_GET["cno"];
    $obj->delete();
    header("Location:index.php");
    exit;
 }


 //first displayay

?>

<input type='button' name='add' value='add' onclick=javascript:location.href='insert.php' />
<table border='1'>
<tr>
  <th>custid</th>
  <th>custname</th>
  <th>custfather</th>
  <th>address</th>
  <th>actions</th>
</tr>
<? $data = $obj->display(); ?>

<? $i = 0;
foreach( $data as $eachrecord ) {
  $i++;
 ?>

<tr>
  <td><? echo $i; ?> </td>
  <td><? echo $eachrecord ['custname']; ?></td>
   <td><? echo $eachrecord ['custfather']; ?></td>
    <td><? echo $eachrecord ['address']; ?></td>
  <td>
      <a href="index.php?cno=<? echo $eachrecord['custid']; ?>" >delete</a> |
      <a href="change.php?cno=<? echo $eachrecord['custid']; ?>">update</a>
  </td>
</tr>
<? } ?>
</table>


insert.php
<form action="index.php" method="post" >
  <table>
    <tr>
      <td>Customer Name</td>
      <td><input type='text' name='custname' id="custname" /></td>
    </tr>
    <tr>
      <td>Father Name</td>
      <td><input type='text' name='cfather' id="cfather" /></td>
    </tr>
    <tr>
      <td>address</td>
      <td><input type='text' name='caddress' id="caddress"/></td>
    </tr>
    <tr>
      <td colspan='3'><input type='submit' name='submit' value='submit'  /></td>
    </tr>
  </table>
</form>