I’m new here. I need to know the problem with these codes since I want to update blog content without image change but I’m stuck with this problem, can you help me to fix my code, please?
if (isset($_POST['edit_blog'])) {
$title = mysqli_real_escape_string($config, $_POST['blog_title']);
$body = mysqli_real_escape_string($config, $_POST['blog_body']);
$filename = $_FILES['blog_image']['name'];
$tmp_name = $_FILES['blog_image']['tmp_name'];
$size = $_FILES['blog_image']['size'];
$image_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$allow_type = ['jpg', 'png', 'jpeg'];
$destination = "upload/".$filename;
$category = mysqli_real_escape_string($config, $_POST['category']);
if (!empty($filename)) {
if (in_array($image_ext, $allow_type)) {
if ($size <= 2000000) {
$unlink = "upload/".$result['blog_image'];
unlink($unlink);
move_uploaded_file($tmp_name, $destination);
$sql3 = "UPDATE blog SET blog_title = '$title', blog_body = '$body', blog_image = '$filename', category = '$category', author_id = '$author_id' WHERE blog_id = '$blogID'";
$query3 = mysqli_query($config, $sql3);
if($query3) {
$msg = ['Update success', 'alert-success'];
$_SESSION['msg'] = $msg;
header('location:index.php');
} else {
$msg = ['Failed, try again', 'alert-danger'];
$_SESSION['msg'] = $msg;
header('location:index.php');
}
} else {
$msg = ['Image should be not greater than 2 MB', 'alert-danger'];
$_SESSION['msg'] = $msg;
header('location:index.php');
}
} else {
$msg = ['This type of image file is not allowed (only jpg, png and jpeg accepted)', 'alert-danger'];
$_SESSION['msg'] = $msg;
header('location:index.php');
}
} else {
$sql3 = "UPDATE blog SET blog_title = '$title', blog_body = '$body', category = '$category', author_id = '$author_id' WHERE blog_id = '$blogID'";
$query3 = mysqli_query($config, $sql3);
if($query3) {
$msg = ['Update success', 'alert-success'];
$_SESSION['msg'] = $msg;
header('location:index.php');
} else {
$msg = ['Failed, try again', 'alert-danger'];
$_SESSION['msg'] = $msg;
header('location:index.php');
}
}
}
I need to know the problem of these codes about the update without image change:
else {
$sql3 = “UPDATE blog SET blog_title = ‘$title’, blog_body = ‘$body’, category = ‘$category’, author_id = ‘$author_id’ WHERE blog_id = ‘$blogID'”;
$query3 = mysqli_query($config, $sql3);
if($query3) {
$msg = [‘Update success’, ‘alert-success’];
$_SESSION[‘msg’] = $msg;
header(‘location:index.php’);
} else {
$msg = [‘Failed, try again’, ‘alert-danger’];
$_SESSION[‘msg’] = $msg;
header(‘location:index.php’);
}
}