0

我正在努力将 clientId 从表单获取到模型。我正在使用 netbeans 11.1 和 php 7.4。我正在关注某个教程,这是我尝试过的,首先是我的模型,它扩展了定义数据库连接的基本模型:


<?php

   class Clients_Model extends Model {


       function __construct() {
           parent::__construct();
           $clientId = filter_input(INPUT_POST, $clientId);
       }

       public function run($clientId = null){
           $statement = $this->db->prepare("SELECT * FROM client WHERE clientId 
                      = :clientId");
           $statement->execute(array(':clientId' => $clientId));
           $data = $statement->fetchAll();
           print_r($data);
       }
   }
?>

接下来是控制器:


<?php

    class Clients extends Controller {

        function __construct() {
            parent::__construct();
        }

        function index(){
            $this->view->render('clients/index');
        }

        function run(){
            $this->model->run();
        }
    }
?>

和观点:

<div id="content">
<h3>Client Booking</h3>
    <form  action="clients/run" method="post">
        <label for="clientId">Client Id:</label><br>
        <input type="text" name="clientId" id="clientId"><br>
        <input type="submit" name="submit"><br>
    </form>
</div>

视图在 render 函数中呈现如下:

<?php

    class View {

        function __construct() {

        }

        public function render($name, $same = false){
            if ($same == true){
                require 'views/' .$name. '.php';
            }else{
                require 'views/header.php';
                require 'views/' .$name. '.php';
                require 'views/footer.php';
            }
        }
    }
?>

错误是get如下:


> Notice: Undefined variable: clientId in C:\xampp\htdocs\healthMentor\models\clients_model.php on line 8
Array ( )


4

1 回答 1

1

这很明显,因为没有像已经提到的错误消息那样定义这样的变量。看着

   function __construct() {
       parent::__construct();
       $clientId = filter_input(INPUT_POST, $clientId);
   }

您希望使用以前从未定义过的变量 $clientId 来定义 $clientId。您确定要在构造函数中包含 clientId 吗?似乎它放错了位置,因为它也从未在进一步的步骤中使用过。也许您想将其移至运行功能?

于 2019-11-14T12:07:44.637 回答