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>

27 comments:

  1. Thanks a lot very nice and very simple is very easy to understand

    ReplyDelete
  2. Please add more Tutorails for OOP PHP.

    ReplyDelete
  3. warring in while($row = mysql_fetch_array($res)) {
    $temp_arr[] =$row;
    Description Resource Path Location Type
    Assignment in condition main.php /edit_delete_insert line 24 DLTK Problem

    ReplyDelete
  4. thanks for sharing such oop tuts

    ReplyDelete
  5. Thanks for sharing this concept of oops. it really nice

    ReplyDelete
    Replies
    1. <?php
      define('DB_SERVER','localhost');
      define('DB_USER','root');
      define('DB_PASS' ,'');
      define('DB_NAME', 'dbtuts');

      class DB_con
      {
      function __construct()
      {
      $conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die('localhost connection problem'.mysql_error());
      mysql_select_db(DB_NAME, $conn);
      }

      public function select()
      {
      $res=mysql_query("SELECT * FROM users");
      return $res;
      }


      public function insert($fname,$lname,$city)
      {
      $res=mysql_query("insert into users (first_name,last_name,user_city) VALUES ('$fname','$lname','$city')");
      return $res;
      }

      public function delete($table,$id)
      {
      $res = mysql_query("DELETE FROM $table WHERE user_id=".$id);
      return $res;
      }

      public function update($table,$id,$fname,$lname,$city)
      {
      $res = mysql_query("UPDATE $table SET first_name='$fname', last_name='$lname', user_city='$city' WHERE user_id=".$id);
      return $res;
      }
      }

      ?>

      Delete
  6. very nice also easy to understand....

    ReplyDelete
  7. very nice tutorial, will u help me to create project in core php using oops?

    ReplyDelete
  8. is very useful for me. Thanks.

    ReplyDelete
  9. very nice tutorial, will u help me to create project in core php using oops?

    ReplyDelete
  10. Thank u very much . A very nice and simple tutorial.. stay Blessed

    ReplyDelete
  11. very nice and simple steps to understand ooops concepts in php..................
    thanks a lllllllllllllllllllll
    ...................................
    ----------------------------
    --------------------
    -----------

    ReplyDelete
  12. thanx for the post...
    it's helpful...

    ReplyDelete
  13. here not updating i'm confused

    ReplyDelete
  14. updating is not working here

    ReplyDelete
  15. update not working here....

    ReplyDelete
  16. How to insert the data using ajax

    ReplyDelete