定义变量时,我得到一个未定义的变量错误


I am getting an Undefined variable error when the variable is defined

定义变量时,我得到一个未定义的变量错误。下面是我的代码

<?php
$imagepath = $_SESSION['path'];
require_once('class-db.php');
if ( !class_exists('INSERT') ) {
    class INSERT {
        public function post($postdata) {
            global $db;
            $query = "
                            INSERT INTO posts (title, content, subcontent, date, category, image)
                            VALUES ('$postdata[title]', '$postdata[content]', '$postdata[subcontent]', '$postdata[date]', '$postdata[category]', '$imagepath')
                        ";
            return $db->insert($query);
        }
    }
}
$insert = new INSERT;
?>

$imagepath在函数外部声明,因此除非将其作为参数传递或使用global关键字,否则它在函数内部不可用。参见可变范围

public function post($postdata, $imagepath) {
    global $db;

public function post($postdata) {
    global $db, $imagepath;