【问题标题】:Create and update in same form [closed]以相同的形式创建和更新[关闭]
【发布时间】:2020-01-27 21:13:29
【问题描述】:

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

致命错误:未捕获的 PDOException:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'TABLE items SET title = 'ewe', text = 'ew', category_id = '1' WHERE 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】:

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

【讨论】:

  • SQL 语法错误消失了,但是值没有更新,知道吗?
  • 您检查过服务器的错误日志@mohsen 吗?
  • @JayBlanchard 你是什么意思? (PHP 初学者)
  • 每个服务器都有错误日志。当某些东西不起作用时,会记录这些错误。
  • 另外,检查/调试更新/创建调用的返回值。假设调用会起作用(就像当前的第一个代码块一样)是一种不好的做法。
猜你喜欢
  • 2021-12-19
  • 2014-02-18
  • 2017-04-02
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 2023-03-26
  • 2019-09-08
  • 2017-07-18
相关资源
最近更新 更多