SQL无法插入外键约束

时间:2017-06-28 14:29:51

标签: php sql

所以我一直在尝试使用insert函数向我的数据库添加注释。 但是它给了我以下错误: 无法添加或更新子行:外键约束失败(project_blok4notes,CONSTRAINT visibility FOREIGN KEY(visibility_id)REFERENCES visibilities({{ 1}})ON UPDATE CASCADE)

visibility_id

这是html表单

public function insert() {
        $errors = [];
        $date = new dateTime();
        $formatted_date = date_format($date, 'Y-m-d H:i:s');
        $author_id = $_SESSION['user_id'];
        $visibility_id = $this->validatedVisibility($errors);
        $title = $this->validatedTitle($errors);
        $description = $this->validatedDescription($errors);
        $student_id = filter_input(INPUT_POST, 'student_id');
        $query =
       "INSERT
        INTO notes (author_id, student_id, creation_date, visibility_id, title, description)
        VALUES (:author_id, :student_id, :formatted_date, :visibility_id, :title, :description)";
      $stmt = $this->pdo->prepare($query);
      $stmt->bindValue(':author_id', $author_id, PDO::PARAM_INT);
      $stmt->bindValue(':formatted_date', $formatted_date, PDO::PARAM_STR);
      $stmt->bindValue(':visibility_id', $visibility_id, PDO::PARAM_STR);
      $stmt->bindValue(':student_id', $student_id, PDO::PARAM_INT);
      $stmt->bindValue(':description', $description, PDO::PARAM_STR);
      $stmt->bindValue(':title', $title, PDO::PARAM_STR);
      $ok = $stmt->execute();
      if (!$ok) {
        $info = $stmt->errorInfo();
        die($info[2]);
      }
    }

0 个答案:

没有答案