Sunday, 15 March 2015

MVC with insert Data Edit and delete update data with Login

index.php
===============
<?php
include_once("controller/controller.php");
$controller = new Controller();// $controller->invoke();$controller->pagehandler();
?>
============
controller.php
============
<?php
require_once('model/service.php');
    class controller {

        private  $service=null;        public function __construct() {            $this->service = new service();       }

        public function pagehandler()        {
            $op = isset($_GET['op']) ? $_GET['op'] : NULL;
            if (!$op || $op == 'a1') {                $this->doaddform();            }             elseif ($op == 'insert') {                $this->insert();          }            elseif($op == 'pro')            {                $this->profile();            }            elseif($op == 'delete')            {                $this->profiledel();            }            elseif($op== 'edit')            {                $this->profileedit();            }            elseif($op == 'update')            {                $this->profileupdate();            }            elseif($op=='login')            {
                $this->log();            }        }        //End of page Handler        function doaddform()        {            include 'view/login.php';        }        function insert()        {
            $data1=$_POST;            $this->service->insertdata($data1);        }        function profile()        {            $row1=$this->service->showpro();            include 'view/profile.php';        }        function profiledel()        {          $data=$_GET['id'];            $row11=$this->service->delpro($data);            include('view/profile.php');        }        function  profileedit()        {
            $data=$_GET['id'];
            $row1=$this->service->editpro($data);            include('view/edit.php');        }        function  profileupdate()        {
            $data=$_POST;
            $row1=$this->service->uppro($data);
        }        function log()        {            if(@$_POST)            {                $data=$_POST;                $this->service->loginchk($data);            }            else            {                include('view/profile.php');            }

        }    }?>
====================
service
====================
<?php
require_once 'model/querys.php';class service{    private $querys = NULL;
    public function __construct()    {        $this->querys = new querys();    }
    private function openDb()    {        if (!mysql_connect("localhost", "root", "")) {            throw new Exception("Connection to the database server failed!");        }        if (!mysql_select_db("mitul")) {            throw new Exception("No contacts database found on database server.");        }    }    function  insertdata($data)    {        $this->openDB();        $this->querys->insert($data);    }    function  showpro()    {        $this->openDB();        $row2=$this->querys->showprofile();
        return $row2;    }    function delpro($data)    {        $this->openDb();        $row22=$this->querys->datadel($data);        return $row22;    }    function  editpro($data)    {

        $this->openDb();        $row22=$this->querys->dataedit($data);
        return $row22;    }    function  uppro($data)    {

        $this->openDb();        $this->querys->dataup($data);

    }    function loginchk($data)    {        $this->openDb();        $this->querys->chk($data);    }}


?>

=============
model part in querys.php 
============================
<?phpclass querys {
    function  insert($data)    {        $sql="insert into addmvc values('','".$data['txtname']."','".$data['txtmname']."','".$data['txtlname']."','".$data['txtpname']."','".$data['txtpass']."')";        mysql_query($sql);//        echo "hello";       // echo "<script>alert('insert');document.location='index.php?op=addform'</script>";        echo "<script>alert('insert');</script>";        header('location:index.php?op=pro');


    }    function showprofile()    {        $sql1="select * from addmvc";        $x=mysql_query($sql1);       while($data=mysql_fetch_array($x)) {
              $row[] = $data ;       }        return $row;

    }    function  datadel($data)    {
        $sql="delete from addmvc where id='".$data."'";        mysql_query($sql);        header('location:index.php?op=pro');    }    function  dataedit($data)    {        //$sql="update addmvc set fname='".$data['txtname']."',mname='".$data['txtmname']."',lname='".$data['txtlname']."' where id='".$data."' ";        $sql="select * from addmvc where id='".$data."'";
       $data1= mysql_query($sql);        $give=mysql_fetch_array($data1);    return $give;


    }    function  dataup($data)    {        $sql="update addmvc set fname='".$data['txtfname']."',mname='".$data['txtmname']."',lname='".$data['txtlname']."',subject='".$data['txtsub']."',pass='".$data['txtpass']."' where id='".$data['hide']."' ";
        mysql_query($sql);        header('location:index.php?op=pro');




    }
//    public function check($data)//    {//        if(@$data['email']=='admin@id.com' and $data['pass']=='admin')//        {//            session_start();//            $_SESSION['id']='admin';//            header("Location:index.php?op=checktable");//        }//        else//        {//            $sql="select * from reg where email='".$data['eid']."' and pass='".$data['pass']."'";//            $result=mysql_query($sql);//            $data=mysql_fetch_array($result);//            if(@$data)//            {//                session_start();//                $_SESSION['id']=$data['rid'];//                header("Location:index.php?op=profile");//            }//        }
    function chk($data)    {       $sql="select * from addmvc WHERE  fname='".$data['txtfname']."' and pass='".$data['txtpass']."'";        $result=mysql_query($sql);        $data=mysql_fetch_array($result);        if($data)        {            header("location:index.php?op=pro");        }        else        {
            echo "wrong";        }
    }





}

?>
===============
addform.php
===============

<html>
<head>
    <title>Add Form</title>
</head>

    <body>
        <form method="post" action="index.php?op=insert"  >

            <table border="1">
                <tr>
                    <th>Fname</th>
                    <td><input type="text" name="txtname"> </td>
                </tr>
                <tr>
                    <th>Middle Name</th>
                    <td><input type="text" name="txtmname"></td>
                </tr>
                <tr>
                    <th>Last Name</th>
                    <td><input type="text" name="txtlname"></td>
                </tr>
                <tr>
                    <th>Subject</th>
                    <td><input type="text" name="txtpname"></td>
                </tr>
                <tr>
                    <th>Password</th>
                    <td><input type="password" name="txtpass"> </td>
                </tr>

                <tr>
                    <td colspan="2" align="center"><input type="submit" name="btnadd" value="ADD"></td>
                </tr>
            </table>
        </form>

    </body>


</html>
=====================
edit.php in view part
=====================
<?php
?>
<html>
<head>
    <title>
        Edit
    </title>
</head>
    <body>
        <form method="post" action="index.php?op=update">
            <table>
                <tr>
                    <th>Fname</th>
                    <th>Fname</th>
                    <th>Fname</th>
                    <th>Subject</th>
                    <th>Password</th>

                </tr>

                <tr>
                    <input type="hidden" name="hide" value="<?php echo $row1[0]; ?>">
                    <td><input type="text" name="txtfname" value="<?php echo $row1['fname']; ?>"> </td>
                    <td><input type="text" name="txtmname" value="<?php echo $row1[2]; ?>"> </td>
                    <td><input type="text" name="txtlname" value="<?php echo $row1[3]; ?>"> </td>
                    <td><input type="text" name="txtsub" value="<?php echo $row1[4];  ?>"> </td>
                    <td><input type="text" name="txtpass" value="<?php echo $row1[5]; ?>"></td>
                </tr>
                <tr>
                    <td><input type="submit" name="btnupdate"> </td>
                </tr>
            </table>

        </form>
    </body>
</html>

==================
login.php
===================
<?php?>
<html>
<head>
    <title>
        Login
    </title>
</head>
<body>
    <form name="f1" method="post" action="index.php?op=login">

        <table border="1">
            <tr>
                <th>user</th>
                <td><input type="text" name="txtfname"> </td>
            </tr>
            <tr>
                <th>Password</th>
                <td><input type="text" name="txtpass"></td>
            </tr>
            <tr>
                <td><input type="submit" name="btnlog" value="Login"></td>
            </tr>
        </table>


    </form>


</body>

</html>
====================
profile.php
====================
<html>
<head>
    <title>SHOW</title>
</head>

<body>
<form method="post">
    <table border="1">
<!--        --><?php//        echo '<pre>';//        print_r($row1);//        die;//        ?>

            <tr>
                <th>First Name</th>
                <th>Middle Name</th>
                <th>Last Nmae</th>
                <th>Subject</th>
                <th>Password</th>
                <th>Action</th>

            </tr>
        <?php        for($i=0;$i<count($row1);$i++) {            ?>
            <tr>
                <td><?php echo $row1[$i][1]; ?></td>
                <td><?php echo $row1[$i][2]; ?></td>
                <td><?php echo $row1[$i][3]; ?></td>
                <td><?php echo $row1[$i][4]?></td>
                <td><?php echo $row1[$i][5]?></td>

                <td><a href="index.php?op=delete&&id=<?php echo $row1[$i][0] ?> ">Delete</a> </td>
                <td><a href="index.php?op=edit&&id=<?php echo $row1[$i][0] ?> ">Edit</a> </td>
            </tr>
        <?php        }?>
        <tr>
            <td colspan="4"><a href="index.php">Registration</a> </td>

        </tr>

    </table>
</form>
</body>

</html>

No comments:

Post a Comment