【发布时间】:2017-01-06 11:57:44
【问题描述】:
我编写了简单的插入数据代码,但每当使用!empty($author) 时,它都会给我这个错误Fatal error: Call to a member function Create() on boolean,但我删除了对!empty($author) 的检查,所以它工作正常。我真的不明白这是给出这个错误以及它的含义。
这是我的代码注释类
class Comment extends DatabaseObject{
// Attributes
protected static $TableName = 'comment';
protected static $DBFields = array('id','author','comment','created','photograph_id');
public $id;
public $author;
public $comment;
public $created;
public $photograph_id;
// Create Comment
public static function Make($photograph_id,$author='Anonymous',$body=''){
if(!empty($photograph_id) && !empty($author) && !empty($body)){
$Comment = new Comment();
$Comment->author = $author;
$Comment->comment = $body;
$Comment->photograph_id = (int)$photograph_id;
$Comment->created = date("Y-m-d H:i:s",time());
return $Comment;
}else{
return FALSE;
}
}
// Find Comment Related Picture
public static function CommentOfPicture($photograph_id){
global $db;
$Comment = static::FindByQuery("SELECT * FROM ".static::$TableName." WHERE `photograph_id`='".$db->EscapeValue($photograph_id)."' ORDER BY created ASC");
return $Comment;
}
}
这是我的表单提交代码
// Comment Submit
if(isset($_POST['submit'])){
$Name = trim($_POST['name']);
$Body = trim($_POST['comment']);
if(!empty($Body) || !empty($Name)){
$Comment = Comment::Make($Photo->id,$Name,$Body);
if($Comment->Create()){
$Session->MSG("Success: Comment is submit, awaiting for approval");
RedirectTo("photo.php?id={$Photo->id}");
}else{
$Session->MSG("Danger: Something is Wrong");
RedirectTo("photo.php?id={$Photo->id}");
}
}else{
$Session->MSG("Danger: Comment is empty");
RedirectTo("photo.php?id={$Photo->id}");
}
}
【问题讨论】:
-
检查
$_POST['name']是否有空字符串。你以name提交什么? -
我正在提交作者姓名。
input text如果将其留空,则默认情况下应在其中写入Anonymous,但事实并非如此。它在其中插入空白字段
标签: php post form-submit