【问题标题】:Query Failed You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax查询失败 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册以获取正确的语法
【发布时间】:2019-01-31 03:12:02
【问题描述】:

我在将内容更新到数据库时遇到了这个问题。错误是:

查询失败您的 SQL 语法有错误;检查 与您的 MariaDB 服务器版本相对应的手册 'post_content = 'kjgljkkjhklj', post_image = 附近使用的语法 第 1 行的“45739895_2381062595269282_8123898”

这是我的全部代码:

if (isset($_GET['p_id'])) {
    $the_post_id = $_GET['p_id'];
}

$query = "SELECT * FROM posts WHERE post_id = $the_post_id";
$select_posts_by_id = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($select_posts_by_id)) {
    $post_id = $row['post_id'];
    $post_author = $row['post_author'];
    $post_title = $row['post_title'];
    $post_category_id = $row['post_category_id'];
    $post_status = $row['post_status'];
    $post_image = $row['post_image'];
    $post_content = $row['post_content'];
    $post_tags = $row['post_tags'];
    $post_comment = $row['post_comment_count'];
    $post_date = $row['post_date'];
}


// if update post button is clicked
if (isset($_POST['update_post'])) {

    $post_author = $_POST['post_author'];
    $post_title = $_POST['post_title'];
    $post_category_id = $_POST['post_category'];
    $post_status = $_POST['post_status'];
    $post_image = $_FILES['image']['name'];
    $post_image_temp = $_FILES['image']['tmp_name'];
    $post_content = $_POST['post_content'];
    $post_tags = $_POST['post_tags'];


    move_uploaded_file($post_image_temp, "../images/{$post_image}");

    if (empty($post_image)) {
        $query = "SELECT * FROM posts WHERE post_id = $the_post_id ";

    }

    $query = "UPDATE posts SET ";
    $query .= "post_title = '{$post_title}', ";
    $query .= "post_category_id = '{$post_category_id}', ";
    $query .= "post_date = now(), ";
    $query .= "post_author = '{$post_author}', ";
    $query .= "post_status = '{$post_status}', ";
    $query .= "post_tags = '{$post_tags}' ";
    $query .= "post_content = '{$post_content}', ";
    $query .= "post_image = '{$post_image}' ";
    $query .= "WHERE post_id = {$the_post_id}"; // this is from the get request


    $update_post = mysqli_query($connection, $query);

    confirmQuery($update_post);

}

这是下面的表格:

<form action="" method="POST" enctype="multipart/form-data">

    <div class="form-group">
        <label for="title">Post Title</label>
        <input value="<?php echo $post_title; ?>" type="text" name="post_title" class="form-control" required="true">
    </div>

    <div class="form-group">
        <label for="post_category">Post Categories</label>
        <select name="post_category" id="" class="form-control form-control-md">
            <?php 
                $query = "SELECT * FROM categories";
                $select_categories = mysqli_query($connection, $query);

                confirmQuery($select_categories); // this is from functions.php

                while ($row = mysqli_fetch_assoc($select_categories)) {
                        $cat_id = $row['cat_id'];
                        $cat_title = $row['cat_title'];

                        echo "<option value='{$cat_id}'>{$cat_title}</option>";
                    }   
             ?>
        </select>
    </div>

    <div class="form-group">
        <label for="author">Post Author</label>
        <input value="<?php echo $post_author; ?>" type="text" name="post_author" class="form-control" required="true">
    </div>

    <div class="form-group">
        <label for="post_status">Post Status</label>
        <input value="<?php echo $post_status; ?>" type="text" name="post_status" class="form-control" required="true">
    </div>

    <div class="form-group">
        <label for="image">Post Image</label>
        <img width="100px" src="../images/<?php echo $post_image; ?>">
        <input type="file" name="image" required="true">
    </div>

    <div class="form-group">
        <label for="post_tags">Post Tags</label>
        <input value="<?php echo $post_tags; ?>" type="text" name="post_tags" class="form-control" required="true">
    </div>

    <div class="form-group">
        <label for="post_content">Post Content</label> 
        <textarea name="post_content" class="form-control" id="" cols="30" rows="10" required="true">
            <?php echo $post_content; ?>
        </textarea>
    </div>

    <div class="form-group">
        <input type="submit" name="update_post" class="btn btn-primary" value="Update Post">
    </div>
</form>

【问题讨论】:

  • 您的 SQL 代码在 post_tags 和 post_image 值之后缺少 , 。
  • @Jarzon 是的,不,post_image 之后没有逗号,因为 WHERE 是下一个
  • 这对 SQL 注入开放。参数化。
  • 那么缺少什么代码?你能具体说明应该添加哪些代码吗?谢谢:)

标签: php mysql database crud


【解决方案1】:

FWIW,我觉得这更容易阅读:

$query = "
UPDATE posts 
   SET post_title = '$post_title'
     , post_category_id = '$post_category_id'
     , post_date = now()
     , post_author = '$post_author'
     , post_status = '$post_status'
     , post_tags = '$post_tags'
     , post_content = '$post_content'
     , post_image = '$post_image'
 WHERE post_id = $the_post_id;
";

...但是用适当的参数化查询替换这些字符串也很重要

【讨论】:

    【解决方案2】:

    尝试替换

    $query .= "WHERE post_id = {$the_post_id}";
    

    $query .= "WHERE post_id = {$the_post_id} ";
    

    这应该会变魔术。

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2021-05-12
      • 2018-11-27
      • 2017-02-11
      • 2019-10-24
      • 2016-06-17
      • 2020-02-22
      • 1970-01-01
      • 2020-02-05
      相关资源
      最近更新 更多