duandi6531 2016-10-12 12:25
浏览 27

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

I'm using MVC structure for simple form to submit data into db and retrieving data from db,but it does not works, please help me to solve this..

This my index.php code:

     <?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>

This my controller.php code:

    <?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');           
      }      
    } 
    ?>

This my model.php code:

    <?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;
      }      
    }
    ?>

This my view.php code:

    <?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);
      }       
    }

    ?>

This my home.html code:

    <!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条回答

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)