定义变量时,我得到一个未定义的变量错误。下面是我的代码
<?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;