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 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题