如何使用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>
douba05167
douba05167 你的代码真的很糟糕-它没有流量或连接。在尝试创建自己的MVC系统之前,请考虑学习一些PHP教程。
接近 4 年之前 回复
dongyoulou4829
dongyoulou4829 LittleBobby说你的脚本存在SQL注入攻击的风险..即使转义字符串也不安全!
接近 4 年之前 回复
doumei1772
doumei1772 一件事你使用$link而不是$conn。$conn似乎是您与数据库的连接。
接近 4 年之前 回复
duan_88598
duan_88598 建议我,如果我需要更改代码中的某些更改,这个结构是否正确..
接近 4 年之前 回复
dongyan1993
dongyan1993 请停止使用mysql_*函数。这些扩展已在PHP7中删除。了解PDO和MySQLi的预处理语句并考虑使用PDO,这非常简单。
接近 4 年之前 回复
douyuan1752
douyuan1752 即时通讯没有收到任何错误,但是当我在输入表单后提交它只是刷新页面..i无法将数据存储到我的数据库中
接近 4 年之前 回复
douri4459
douri4459 它怎么行不通?你收到错误了吗?你检查过错误日志了吗?
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问