PHP MYSQL映像更新无法正常运行,并显示“您的SQL语法有错误". [英] PHP MYSQL image update is not working saying "You have an error in your SQL syntax"
问题描述
这让我发疯了!我已经住了2个晚上,试图解决这个错误.我还在整个"Google"中搜索了这个问题,似乎找不到正确的答案.
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.
我想使用PHP更新图像.该代码似乎与错误消息唯一的例外一起工作:
I want to update image using PHP. The code seems to be working with the sole exception of the error message that says:
"43ERROR:无法执行1.您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在第1行的'1'附近使用正确的语法."
"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>
推荐答案
答案(相当)是一个简单的答案,对您来说可能听起来很奇怪,但是从某种意义上说,您的查询实际上已经执行了.
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.
现在,按照right syntax to use near '1'
(错误)消息得到1
的原因是,您两次使用mysqli_query()
,就在这里:
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";
}
您需要做的是将if
语句更改为:
What you need to do is to change that if
statement to:
if($edit){
// handle your method here.
}
顺便说一句,您愿意进行认真的sql注入;如果您重视自己的工作和用户群,请使用准备好的语句.
Btw, you're open to a serious sql injection; use a prepared statement if you value your work and userbase.
这篇关于PHP MYSQL映像更新无法正常运行,并显示“您的SQL语法有错误".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!