【问题标题】:Downloading from the database to form PHP从数据库下载形成PHP
【发布时间】:2015-08-02 03:37:59
【问题描述】:

我希望在我的应用程序中添加“发票”时可以查看数据库中的客户,因此我不必输入整个名称,而不仅仅是输入一个选择输入,我会在其中即时获取客户,我不知道如何按照MVC解决。

控制器/invoice.php

<?php

include 'Controller/controller.php';

class InvoicesController extends Controller{
    public function index() {
        $view=$this->loadView('invoices');
        $view->index();
    }
    public function add() {
        $view=$this->loadView('invoices');
        $view->add();
    }
    public function insert() {
        $model=$this->loadModel('invoices');
        $model->insert($_POST);
        $this->redirect('?task=invoices&action=index');
    }
    public function delete() {
        $model=$this->loadModel('invoices');
        $model->delete($_GET['id']);;
        $this->redirect('?task=invoices&action=index');
    }
    public function edit() {
        $model=$this->loadView('invoices');
        $model->edit($_GET['id']);
    }
    public function getClients(){
        $model = $this->loadModel('clients');
        $model->getAll();        
    }
    public function editInvoice(){
        $model = $this->loadModel('invoices');
        $model->editInvoice($_POST);
        $this->redirect('?task=invoices&action=index');

    }

}

型号/invoices.php

<?php

include 'Model/model.php';


class InvoicesModel extends Model{


public function insert($data) {

    $ins=$this->pdo->prepare("INSERT INTO invoices (id,Number,Name,Service,Symbol,Unit, Tax, Value)"
            . " VALUES ('',:number,:name,:service,'szt','2',:tax,:value)");
    $ins->bindValue(':name', $data['name'], PDO::PARAM_STR);
    $ins->bindValue(':number', $data['number'], PDO::PARAM_STR);
    $ins->bindValue(':service', $data['service'], PDO::PARAM_STR);
    $ins->bindValue(':tax', $data['tax'], PDO::PARAM_STR);
    $ins->bindValue(':value', $data['value'], PDO::PARAM_STR);
    $ins->execute();
}
public function getAll() {
    return $this->select('invoices');
}
public function getId() {
    return $this->getLastId('invoices');
}
public function edit($id) {
    return $this->select('invoices',' * ',' id = '.$id.' ');
}
public function editInvoice($data){
    $edit=$this->pdo->prepare("UPDATE invoices SET Name = :name WHERE id = :id");
    $edit->bindValue(':name', $data['name']);
    $edit->bindValue(':id', $data['id']);
    $edit->execute();
}
public function delete($id) {
    $del=$this->pdo->prepare('DELETE FROM invoices where id=:id');
    $del->bindValue(':id', $id, PDO::PARAM_INT);
    $del->execute();
}

}

查看/invoice.php

<?php

include 'View/view.php';

class InvoicesView extends View{
    public function  index() {
        $cat=$this->loadModel('invoices');
        $this->set('catsData', $cat->getAll());
        $this->render('indexCategory');
    }
    public function  add() {
        $inv=$this->loadModel('invoices');
        $this->set('invoicesData', $inv->getLastId('invoices'));

        $this->render('addCategory');  
    }
    public function getClients(){
        $get = $this->loadModel('clients');
        $this->set('clientsData', $get->getClients());
        $this->render('addCategory');
    }
    public function edit($id){
        $edit = $this->loadModel('invoices');
        $this->set('invoicesData', $edit->edit($_GET['id']));
        $this->render('editInvoice');
    }
}

和 模板/AddCategory.html.php

<?php include 'templates/header.html.php'; ?></pre>
<h1>Dodaj kontrahenta</h1>
<?php 

?>
<form action="?task=invoices&action=insert" method="post">
    Nazwa kontrahenta: <input type="text" name="name" /><br>
    NIP: <input type="text" name="nip" /><br>
    Adres: <input type="text" name="adress" /><br>
    Kod Pocztowy: <input type="text" name="postcode" /><br>
    Miasto: <input type="text" name="city" /><br>
    Telefon: <input type="text" name="phone" /><br>
    Email: <input type="text" name="email" /><br>
 <input type="submit" value="Dodaj" /></form>
<pre>
<?php include 'templates/footer.html.php'; ?>

如果我这样做了

<?php include 'templates/header.html.php'; ?></pre>
<h1>Dodaj kontrahenta</h1>
<?php 
$data = $this->set('clientsData', $get->getClients());
 var_dump($data);
?>
<form action="?task=invoices&action=insert" method="post">
    Nazwa kontrahenta: <input type="text" name="name" /><br>
    NIP: <input type="text" name="nip" /><br>
    Adres: <input type="text" name="adress" /><br>
    Kod Pocztowy: <input type="text" name="postcode" /><br>
    Miasto: <input type="text" name="city" /><br>
    Telefon: <input type="text" name="phone" /><br>
    Email: <input type="text" name="email" /><br>

 <input type="submit" value="Dodaj" /></form>
<pre>
<?php include 'templates/footer.html.php'; ?>

我收到错误

Notice: Undefined variable: get in /home/krytykak/domains/jozwiak.edu.pl/public_html/templates/addCategory.html.php on line 4

Fatal error: Call to a member function getClients() on a non-object in /home/krytykak/domains/jozwiak.edu.pl/public_html/templates/addCategory.html.php on line 4

【问题讨论】:

  • 为什么在最后一个文件中提到$get?应该在哪里定义?

标签: javascript php mysql ajax model-view-controller


【解决方案1】:
$get->getClients()

$get 没有在任何地方定义。您正在调用 getClients() ,它是 InvoicesController 对象的成员。所以我在这里想$get = new InvoicesController();应该在做$data = $this-&gt;set('clientsData', $get-&gt;getClients());之前初始化

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 2013-01-03
    • 1970-01-01
    • 2011-09-06
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多