以相同格式创建和更新[关闭]

问题描述 投票:-3回答:1

我正在尝试使用相同的视图来创建和更新条目,创建工作正常,但是当我尝试更新时,出现此SQL语法错误,但在模型中看不到语法错误

致命错误:未被捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参阅附录A。请检查与您的MySQL服务器版本相对应的手册,以获取正确的语法,以在“ TABLE article SET SET title ='ewe',text ='ew',category_id ='1'的情况下在第1行使用id ='1'']

<?php

    use App\Model\ArticleModel;

    if(isset($article)) { 
      $id = $article['id'];
      $title = $article['title'];
      $text = $article['text'];
      $category_id = $article['category_id'];

      $action = "?page=form&id=$id";
    }

    if (!empty($_POST)) {
      $id = $_GET['id'];
      $title = $_POST['title'];
      $text = $_POST['text'];
      $category_id = $_POST['category_id'];

      if(isset($_GET['id'])) {
        $this->modelName->update($id,$title, $text, $category_id);
        // $this->modelName is actually correct, it is defined in the core
      } else {
        $this->modelName->create($title, $text, $category_id);
      }
      header("Location: index.php");
    }
?>

<div class="container">
<h1>Save Article</h1>
<form action="<?= isset($action)?$action:'?page=form' ?>" method="post">
  <div class="form-group">
    <label for="title">Title</label>
    <input name="title" value="<?= !empty($title)?$title:'' ?>" type="text" class="form-control" id="title" aria-describedby="title">
  </div>
  <div class="form-group">
    <label for="text">Text</label>
    <textarea name="text" class="form-control" id="text"><?= !empty($text)?$text:'' ?></textarea>
  </div>
  <div class="form-group">
    <label for="category">Category</label>
    <select name="category_id" value="<?= !empty($category_id)?$category_id:'' ?>" id="category" class="form-control">
        <?php foreach ($categories as $category): ?>
            <option value="<?= $category["id"] ?>" <?php if(isset($article) && $category["id"] == $article['category_id']) { echo 'selected="selected"';} ?>><?= $category["name"] ?></option>
        <?php endforeach; ?>
    </select>
  </div>
  <button type="submit" class="btn btn-primary">Save</button>
</form>
</div>

ArticleModel

public function create($title, $text, $category_id) {
        return $this->db->save(
            'INSERT INTO articles SET title = ?, text = ?, category_id = ?', [$title, $text, $category_id]
        );
    }

public function update($id, $title, $text, $category_id) {
        return $this->db->save(
            "UPDATE TABLE articles SET title = ?, text = ?, category_id = ? WHERE id = ?", [$title, $text, $category_id, $id]
        );
    }

我怀疑错误来自$ action和$ _GET ['id'],但我找不到解决方法

php pdo
1个回答
1
投票

在您的更新功能中。这是无效的SQL语法UPDATE TABLE articles ...。它应显示为UPDATE articles ...

© www.soinside.com 2019 - 2024. All rights reserved.