mysql映像更新不起作用,它说“您的sql语法有错误”

xoefb8l8  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(301)

我快疯了!为了解决这个错误,我已经呆了两个晚上了。我也把这个问题搜遍了“谷歌”似乎找不到正确答案。
我想用php更新图片。这段代码似乎只有一个例外,错误信息是:
“错误:无法执行1。sql语法有错误;请查看与您的mysql服务器版本对应的手册,以获取在第1行“1”附近使用的正确语法。
请帮帮我!我将非常感激

<?php include('../db_connect.php'); 
echo $id = $_GET['id'];

$sql = mysqli_query($con, "
SELECT * 
  FROM `blog_posts` 
 WHERE post_id='$id'");
$row = mysqli_fetch_array($sql);

    //-------------------WHEN SUBMIT BUTTON IS CLICKED------------------------
    if(isset($_POST['submit'])){
        $post_title = $_POST['posttitle'];
        $content = $_POST['content'];
        $author_name = $_POST['authorname'];
        $category = $_POST['category'];
        if(isset($_FILES['image']['name']) && ($_FILES['image']['name'] !="")){
            $size=$_FILES['image']['size'];
            $temp=$_FILES['image']['tmp_name'];
            $type=$_FILES['image']['type'];
            $image_name=$_FILES['image']['name'];
            unlink("../images/"."$image_name");

            move_uploaded_file($temp,"../images/$image_name");
        }

    //-------------------UPDATE POST------------------------

        $edit = mysqli_query($con, "
UPDATE blog_posts 
   SET post_title='$post_title'
     , content='$content'
     , author_name='$author_name'
     , category='$category'
     , post_date=now()
     , image='$image_name' 
 WHERE post_id='$id'
");
        if(mysqli_query($con, $edit)){
            echo "date updated successfully";
        } else{
            echo "ERROR: Could not able to execute $edit. " . mysqli_error($con);
        }
    }

?>

<form action="edit.php?id=<?php echo $row['post_id']; ?>" method="post" enctype="multipart/form-data">      
                <input type="hidden" name="size" value="1000000" />
                <input type="text" name="posttitle" value="<?php echo $row['post_title'];?>" /><br />
                <textarea name="content"><?php echo $row['content'];?></textarea><br />
                <input type="text" name="authorname" value="<?php echo $row['author_name'];?>"/><br />
                <input type="text" name="category" value="<?php echo $row['category'];?>"><br />
                <img src="../images/<?php echo $row['image'];?>" />
                <input type="file" name="image" /><br />
                <button type="submit" name="submit" >Post</button>                  
            </form>
am46iovg

am46iovg1#

答案(相当)简单,对您来说可能听起来很奇怪,但从某种意义上说,这意味着您的查询确实执行了。
现在,你这么做的原因是 1 根据 right syntax to use near '1' (错误)消息,是您使用的 mysqli_query() 两次,就在这里:

$edit = mysqli_query($con, "
                ^^^^^^^^^^^^ Here
UPDATE blog_posts 
   SET post_title='$post_title'
     , content='$content'
     , author_name='$author_name'
     , category='$category'
     , post_date=now()
     , image='$image_name' 
 WHERE post_id='$id'
");
        if(mysqli_query($con, $edit)){
           ^^^^^^^^^^^^ and here
            echo "date updated successfully";
        }

你需要做的是改变这种状况 if 声明收件人:

if($edit){
// handle your method here.
}

顺便说一句,你对sql注入持开放态度;如果你重视你的工作和用户群,使用一个准备好的语句。
https://en.wikipedia.org/wiki/prepared_statement

相关问题