【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or updateSQLSTATE [23000]:完整性约束违规:1452 无法添加或更新
【发布时间】:2020-09-29 15:30:29
【问题描述】:

我有这个问题:

SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(tutoblog.post_category,CONSTRAINT fk_category FOREIGN KEY(category_id)参考category (id) 更新级联删除级联)

【问题讨论】:

  • 公共函数 attachCategories(int $id,array $categories){ $this->pdo->exec('DELETE FROM post_category WHERE post_id = ' . $id); $query= $this->pdo->prepare('INSERT INTO post_category SET post_id = ?,category_id = ?'); foreach ($categories as $category){ $query->execute([$id, $category]); } }
  • 公共函数选择(字符串 $key,字符串 $label,数组 $options = []):字符串 { $optionsHTML = []; $value = $this->getValue($key); foreach ($options as $K => $v) { $selected = in_array($key,$value) ? “选定”:“”; $optionsHTML[] = "$v" ; } $optionsHTML = implode('', $optionsHTML);返回 {$this->getErrorFeedback($key)}
    HTML;

标签: php


【解决方案1】:

您正在尝试插入/更新具有外键约束的表。

字段category_id 必须设置为您的表category 中存在的ID。 因此,请务必先创建类别,然后将其链接到您的条目中。

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 2016-05-31
    • 2018-04-17
    • 2020-01-23
    • 2021-02-14
    • 2018-02-12
    • 2021-09-29
    相关资源
    最近更新 更多