PHP MYSQL 图片更新不工作说 "You have an error in your SQL syntax"
PHP MYSQL image update is not working saying "You have an error in your SQL syntax"
这让我发疯!我已经住了 2 晚试图解决这个错误。我也搜索了这个问题 "Google" 似乎找不到正确的答案。
我想使用 PHP 更新图像。该代码似乎在处理错误消息的唯一例外情况:
"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".
请帮帮我!我会非常感激的。:)
<?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>
答案(相当)简单,您可能听起来很奇怪,但从某种意义上说,这意味着您的查询确实执行了。
现在,根据 right syntax to use near '1'
(错误)消息,您收到 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 注射;如果您重视您的工作和用户群,请使用准备好的声明。
这让我发疯!我已经住了 2 晚试图解决这个错误。我也搜索了这个问题 "Google" 似乎找不到正确的答案。
我想使用 PHP 更新图像。该代码似乎在处理错误消息的唯一例外情况:
"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".
请帮帮我!我会非常感激的。:)
<?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>
答案(相当)简单,您可能听起来很奇怪,但从某种意义上说,这意味着您的查询确实执行了。
现在,根据 right syntax to use near '1'
(错误)消息,您收到 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 注射;如果您重视您的工作和用户群,请使用准备好的声明。