【问题标题】:Inserting a Comment插入评论
【发布时间】:2018-09-14 14:42:41
【问题描述】:

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

// 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;
}

properties() 和 cleanProperties 只是查找每个对象的属性,在这种情况下,comment-object 和 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() 方法内部的某个地方犯了一个错误,在为用户、照片等测试它时不可见。

【问题讨论】:

  • 您是否正在尝试设计自己的 ORM?为什么不使用现有的库?
  • 我想查看 $query 的值,即您实际执行的 SQL 以查看哪里出了问题。
  • 插入的sql是:INSERT INTO cmets(photo_id, author, and many) VALUES(0, 'author', many more) 评论 ID 会自动递增。
  • 你在零附近有引号吗? VALUES('0','author'... 代码在所有内容周围加上引号,甚至是数字字段 - 它可以解释您的问题
  • 不,没有引号。如前所述,我可以正确地回显它,但是一旦它通过 create() 方法运行,它就会更改为 0。 create() 方法将正确的值应用于属性作者、内容等。唯一可以平衡的东西改变的是 photo_id。

标签: php sql oop insert-into


【解决方案1】:

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

当你使用 $comment->photoId = $photoId; 我认为您在类注释中有名为“photoId”的属性。

所以它通过 cleanProperties() 生成带有 "comment ('photoId',.....)" 的 SQL 字符串 在 create() 函数中

但是在你工作的 SQL 中有 photo_id

检查您在 Class 中的属性定义并将 $comment->photoId 更改为 $comment->photo_id 或将 columnt photo_id 重命名为 照片ID

Class 中 photo ID 的定义,createComment() 中的 photo ID 和 column photo ID 必须同名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 2018-07-29
    • 2019-08-11
    • 2011-12-18
    • 2012-10-11
    • 2014-04-15
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多