插入评论

时间:2018-04-04 22:03:07

标签: php sql oop insert-into

我有一个非常具体的问题,我正在尝试插入与图片相关的评论。所以这是代码:

// GET ID TO INSTANTIATE
$photo = Photo::findById($_GET['id']);
// TESTING PURPOSE
echo $photo->id
$message = "";

// CHECK IF ADD COMMENT IS SET
if(isset($_POST['addComment'])){
$author  = trim($_POST['author']);
$content = trim($_POST['content']);

// CREATE COMMENT
$comment = Comment::createComment($photo->id, $author, $content);
if($comment && $comment->save()){
    redirect("photo.php?id={$photo->id}");
}else{
    $message = "There was a problem uploading the comment!";
}
}else{
$author = "";
$content = "";
}

$comments = Comment::findComments($photo->id);

所以我试图想办法让这个工作两天,我找不到错误。因此,如果我回显$photo->id,结果是例如13,但是当我使用createComment()方法时,数据库中的结果总是0,如果我选择id为1,2,...,50,...

这就是createComment方法的作用:

// CRUD: CREATE
    public static function createComment($photoId, $author="EXAMPLE", $content=""){
        if(!empty($photoId) && !empty($author) && !empty($content)){
            $comment = new Comment();
            $comment->photoId = $photoId;
            $comment->author  = $author;
            $comment->content = $content;
            return $comment;
        }else{
            return false;
        }
    }

我认为问题是$ comment-> save()方法,所以这里是代码:

// CHECK TO UPDATE OR CREATE
public function save(){
    return isset($this->id) ? $this->update() : $this->create();
}

// CRUD: CREATE
public function create(){
    global $database;
    $properties = $this->cleanProperties();
    $query = "INSERT INTO " . static::$dbTable . "(" . implode(",", array_keys($properties)) . ") ";
    $query .= "VALUES('" . implode("','", array_values($properties)) . "')";
    if($database->query($query)){
        $this->id = $database->insertId();
        return true;
    }else{
        return false;
    }
}

// CRUD: UPDATE
public function update(){
    global $database;
    $properties = $this->properties();
    $propertiePairs = array();
    foreach($properties as $key => $value){
        $propertiePairs[] = "{$key}='{$value}'";
    }
    $query = "UPDATE " . static::$dbTable . " SET ";
    $query .= implode(", ", $propertiePairs);
    $query .= " WHERE id= " . $database->escapeString($this->id);
    $database->query($query);
    return (mysqli_affected_rows($database->connection) == 1) ? true : false;
}

属性()和cleanProperties只是发现每个对象的属性,在这种情况下,注释的对象和cleanProperties都正被泄露这些和他们的工作到目前为止我不认为这个问题是在那里,但请告诉我,如果我错了:

   // FIND PROPERTIES
    protected function properties(){
        $properties = array();
        foreach(static::$dbTableFields as $dbField){
            if(property_exists($this, $dbField)){
                $properties[$dbField] = $this->$dbField;
            }
        }
        return $properties;
    }

    // CLEAN PROPERTIES
    protected function cleanProperties(){
        global $database;
        $cleanProperties = array();
        foreach($this->properties() as $key => $value){
            $cleanProperties[$key] = $database->escapeString($value);
        }
        return $cleanProperties;
    }

所以我找到了一个有效的解决方案,但这不是我想要的,如果我用经典的程序化PHP替换createComment()方法,如:

$query = "INSERT INTO comments(photo_id, author, content) VALUES({$photo->id}, '{$author}', '{$content}')";
    $insertComment = mysqli_query(mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME), $query);

一切正常,我在数据库中获得了正确的photo_id值。所以请告诉我,如果你发现任何类型的错误,create()方法和所有其他方法适用于用户,照片等,所以我认为问题显然在createComment()内-方法。另一种可能的方式是我在create()方法的某个地方犯了一个错误,在为用户,照片等进行测试时看不到它。

1 个答案:

答案 0 :(得分:0)

您的项目架构需要具有与列名相同的属性名称。 (参见功能创建)

使用 $ comment-> photoId = $ photoId; 我认为你在Class Comment中有一个名为“photoId”的属性。

所以它通过cleanProperties()用“comment(' photoId ',.....)”生成SQL字符串 在create()函数

但是你在使用SQL

时有 photo_id

检查类中的属性定义,并将 $ comment-> photoId 更改为 $ comment-> photo_id 或将columnt photo_id 重命名为 PHOTOID

类中照片ID的定义,createComment()中的照片ID和列照片ID必须命名为相同。

相关问题