如何使用mvc结构从数据库中存储和检索数据?

时间:2016-10-12 12:25:15

标签: php mysql model-view-controller

我正在使用MVC结构以简单的形式将数据提交到db并从db中检索数据,但它不起作用,请帮我解决这个问题。

这是我的index.php代码:

     <?php 
     include_once('../../../mvc/Controller.php');         
     ?> 
     <html>
     <head>
     <body>

      <form action="index.php?Controller/getAllregform" method="POST">
    Firstname: <input type="text" name="fname"><br>
    Lastname: <input type="text" name="lname"><br>
    Mobile: <input type="text" name="mobile"><br>
    E-mail: <input type="text" name="email"><br>

        <input type="submit" name="submit" value="submit">&nbsp;
        <input type="reset" value="Reset">
    </form>
    </body>
    </head>
    </html>

这是我的controller.php代码:

    <?php
    include_once('../../../mvc/model.php');
    include_once('../../../mvc/View.php');
    class Controller{
      private $model;
       private $view;
      function __construct(){

         $this->model=new Model();
          $this->view =new View();
      }
     public function getAllregform(){              
        $regform = $this->model->getregform();         
        $this->view->assign('regform',$row);           
        $this->view->display('./home.html');           
      }      
    } 
    ?>

这是我的model.php代码:

    <?php       
    class Model {
      private $regform;  

     function __construct(){

          function addfield($data)
          {
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "mvc";

                $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);  
                mysql_select_db(DB_DATABSE, $conn);  
                if(!$conn)
                {  
                    die ("Cannot connect to the database");  
                }   
                return $conn;                

    $fname = mysql_real_escape_string($link, $_POST['fname']);
    $lname = mysql_real_escape_string($link, $_POST['lname']);
    $mobile = mysql_real_escape_string($link, $_POST['mobile']);
    $email = mysql_real_escape_string($link, $_POST['email']);      

        $this->db->query("INSERT INTO mvc (fname, lname,mobile,email)
    VALUES ('$fname', '$lname', '$mobile','$email')");
        $this->addfield($data);
         }
            $this->db->query('select * from mvc');
        $this->regform = array('select * from mvc');         
     } 

     public function getregform(){      
        return $this->regform;
      }      
    }
    ?>

这是我的view.php代码:

    <?php
    class View {
     private $data=array();
      function assign($varname,$vardata){
         $this->data[$varname]=$vardata;
      }
      function display($filename){
        //extract the data that the controller got from the model
        extract($this->data);
        //include the file that presents the extracted data
        include($filename);
      }       
    }

    ?>

这是我的home.html代码:

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Your page title</title>
        <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
    </head>
    <body>
      <ul>
        <?php
        while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
         {
          echo "FIRST NAME :{$row['fname']}  <br> ".
             "LAST NAME : {$row['lname']} <br> ".
             "MOBILE : {$row['mobile']} <br> ".
              "EMAIL : {$row['email']}"
              }
        ?>
        </ul>
    </body>
    </html>

0 个答案:

没有答案