类中未定义的变量错误

时间:2014-05-02 23:20:57

标签: php mysql function class

我有一个在表单内运行的类与数据库通信,但我正在使用的变量返回错误。我搜索过谷歌,但我无法得到明确的答案。这是错误,我可能错过了一些明显的东西......希望:

Notice: Undefined variable: beer_name in /home/carlton/public_html/PHPproject/models/beer.php on line 28

Notice: Undefined variable: beer_type in /home/carlton/public_html/PHPproject/models/beer.php on line 35

Notice: Undefined variable: beer_abv in /home/carlton/public_html/PHPproject/models/beer.php on line 42

Notice: Undefined variable: beer_rating in /home/carlton/public_html/PHPproject/models/beer.php on line 49

Notice: Undefined variable: BeerEditor in /home/carlton/public_html/PHPproject/forms/beeradded.php on line 38

Fatal error: Call to a member function addBeer() on a non-object in /home/carlton/public_html/PHPproject/forms/beeradded.php on line 38

这是我的班级:

protected $beer_name;   //varchar(45)
protected $beer_type;   //varchar(45)
protected $beer_abv;    //decimal(4,2) alcohol percentage ex. 06.50
protected $beer_rating; //char(10) 1 awful beer, 10 great beer


public function __construct($beer_name = null){
    if ($beer_name !== null){
        $this->setBeerName($beer_name);
    }    
    //defaults
    //$this->setBeerType($beer_type);
    //$this->setBeerABV($beer_abv);
    //$this->setBeerRating($beer_rating);


}

    public function setBeerName(){
        $this->beer_name = $beer_name;
    }
    public function getBeerName(){
            return $this->beer_name;
    }

    public function setBeerType(){
        $this->beer_type = $beer_type;
    }
    public function getBeerType(){
        return $this->beer_type;
    }

    public function setBeerABV(){
        $this->beer_abv = $beer_abv;
    }
    public function getBeerABV(){
        return $this->beer_abv;
    }

    public function setBeerRating(){
        $this->beer_rating = $beer_rating;
    }
    public function getBeerRating(){
        return $this->beer_rating;
    }

}

这是我的表格:

<?php
session_start();

ini_set('display_errors', 1);
error_reporting(E_ALL); ini_set('display_errors', 1);

require "/home/carlton/public_html/PHPproject/allincludes.php";

//var_dump($_POST);
if ($_SESSION['logged_in']!="yes"){
    header ("Location: unauth.php");
}



//if (count($_POST)>0){
//need to add in validation check here
//
//
//
//
//$validationErrors= 0;

    //if(count($validationErrors == 0 )){
        $form=$_POST;
        $beer_name = $form['beer_name'];
        $beer_type = $form['beer_type'];
        $beer_abv = $form['beer_abv'];
        $beer_rating = $form['beer_rating'];

   // }

    $new_beer = new Beer($beer_name);
    $new_beer->setBeerType($beer_type);
    $new_beer->setBeerABV($beer_abv);
    $new_beer->setBeerRating($beer_rating);

    $BeerEditor->addBeer($new_beer);

    echo "Beer added.";
 echo '<a href="addbeer.php"> Back to add menu </a>';  
//}

1 个答案:

答案 0 :(得分:1)

你忘了传递参数,在这里:

public function setBeerName(){
   $this->beer_name = $beer_name;
}

它应该是:

public function setBeerName($beer_name){
   $this->beer_name = $beer_name;
}

请检查函数setBeerType,setBeerABV,setBeerRating。