Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

PHP MYSQL image update is not working saying "You have an error in your SQL syntax"

This is driving me crazy! I have stayed for 2 nights trying to solve this error. I also searched this problem all over "Google" can't seem to find the right answer.

I want to update image using PHP. The code seems to be working with the sole exception of the error message that says:

"43ERROR: Could not able to execute 1. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1".

Please help me! I will be very thankful.:)

<?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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer is (fairly) a simple one and it may sound rather odd to you, but that in a sense meant that your query did in fact execute.

Now, the reason you're getting that 1 as per the right syntax to use near '1' (error) message, is that you used mysqli_query() twice, right in here:

        $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";
        }

What you need to do is to change that if statement to:

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

Btw, you're open to a serious sql injection; use a prepared statement if you value your work and userbase.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...