错误插入PHP

时间:2014-05-27 13:02:33

标签: php mysql oop insert

我开始研究OO php,我遇到了一些问题。

在下面的代码中,我试图在银行中进行一次插入。这很简单,但我已经遇到了问题。

当我尝试发送以下错误时出现:

Fatal error: Call to a member function query() on a non-object in C:\test\web\testoo\Aluno.Controle.php on line 24

Aluno.class.php

<?php
Class Aluno{

private $cpf;

    public function setCPF($cpf)
    {
        $this->cpf = $cpf;
    }

    public function getCPF()
    {
        return $this->cpf;
    }
}
?>

Aluno.Controle.php

<?php
include ('Aluno.class.php');
include ('Banco.php');

$cpf=$_POST['cpf'];

Class AlunoControle {

    private $consulta;

    public function AlunoDao() {

        $novaconexao = new Banco();
        $this->consulta = $novaconexao->conectar(); 

    }

    public function CadastrarAluno($cpf) {

        $this->AlunoDao();
        $cadastrar = new Aluno;
        $cadastrar->setCPF($cpf);

        $query = ("INSERT into cadastro values (". $aluno->getCPF() .")");
        $this->consulta->query($query);


    }
}

$pessoa2 = new AlunoControle();
$pessoa2->CadastrarAluno($cpf);

Banco.php

<?php

Class Banco{

    private $host  = "localhost"; 
    private $user  = "root"; 
    private $pass  = "";
    private $banco = "testephp";
    private $MySQLi;


    public function conectar(){
        $MySQLi = new mysqli( $this->host, $this->user, $this->pass, $this->banco );

        if (mysqli_connect_errno()) {
            die(mysqli_connect_error());
            exit();
        }
            echo "Conectado com sucesso!!!";
    }

    public function desconectar() {
        if ($this->MySQLi) {
            mysqli_close ($this->MySQLi); 
        } 
        //echo "Desconectado com sucesso!!!";
    }
}

的index.html

<form id="frm-cadastro" class="frm" method="post" action="Aluno.Controle.php">
                <fieldset>
                    <legend align="center">Cadastro de Notícias</legend>

                    <label for="noticia">
                        Notícia:
                    CPF: <input name="cpf" type="text"><br>

                    </label>

                    <div id="botoes">
                        <input type="submit" name="enviar" value="Enviar" />

                    </div>
                </fieldset>
            </form>

3 个答案:

答案 0 :(得分:1)

在致电private property $consulta之前,您未将值设置为CadastrarAluno

试试这段代码:

Class AlunoControle {

    private $consulta;

    public function __construct() {

        $novaconexao = new Banco();
            $this->consulta = $novaconexao->conectar();

    }

    public function CadastrarAluno($cpf) {

        $this->AlunoDao();
        $cadastrar = new Aluno;
        $cadastrar->setCPF($cpf);

        if ($this->consulta->query("INSERT into cadastro values (".$aluno->getCPF().")")) {
        }
    }
}

在此代码中,您在类构造函数中设置属性consulta

答案 1 :(得分:1)

这应该是

public function __construct() {

        $novaconexao = new Banco();
            $this->consulta = $novaconexao->conectar();

    }

$this->consulta将在创建对象时初始化

答案 2 :(得分:1)

从错误消息中可以看出,$this->consulta属性未设置为对象。

这意味着当你这样做时会出现问题:

$novaconexao = new Banco();
$this->consulta = $novaconexao->conectar();

因此,conectar()方法不会返回对象,或者很可能由于某种原因导致与数据库的连接失败。