从数据库下载到PHP

时间:2015-05-20 18:10:10

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

我想在我的应用程序中添加“invoice”时可以查看数据库中的客户,所以我不必输入整个名称,而只是输入选择输入,我会在那里抓住客户端,我不知道如何解决按照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

1 个答案:

答案 0 :(得分:0)

$get->getClients()

$ get未在任何地方定义。您正在调用getClients(),它是InvoicesController对象的成员。所以我在这里考虑$get = new InvoicesController();应该在$data = $this->set('clientsData', $get->getClients());

之前进行初始化