I am trying to insert the image name in the table (diary) and at the same time I am trying to update the image name in the table (category). but the value is not updating the in the category. I tried all the available solutions on the internet but nothing worked can someone help me?
I want to update the (category) table "cat_img" whenever the user adds a new image to the table (diary) diary_thumbnail_image.
Please help me it will be appreciated.
here is my code add_diary.php
<?php include 'header.php';
if ($admin != 1 && $admin != 2) {
header("location:index.php");
}
if (isset($_SESSION['user_data'])) {
// This will fetch the author id that is stored in index['0'].
$author_id = $_SESSION['user_data']['0'];
}
$sql = "SELECT * FROM categories";
$query = mysqli_query($config, $sql);
$sql_school = "SELECT * FROM school";
$query_school = mysqli_query($config, $sql_school);
?>
<form method="POST" action="" enctype="multipart/form-
data"
this diary:</strong></label>
<select class="form-control"
name="show_in_school">
<option value=""
name="show_in_school">Select School
Name</option>
<!-- $cats will fetch all the data that
is stored in the categories. -->
<?php while ($school_result =
mysqli_fetch_assoc($query_school)) { ?>
<option value="<?=
$school_result['school_title'] ?>">
<?= $school_result['school_title'] ?>
</option>
<?php } ?>
}
</select>
</div>
<div class="mb-3">
<input type="submit" name="add_diary"
value="Add" class="btn btn-primary">
<a class="btn btn-secondary"
href="diary_information.php">Back</a>
</div>
</form>
<?php
if(isset(add_diary)){
$select_cat = "SELECT * FROM categories";
$query_cat = mysqli_query($config, $select_cat);
$cat_result = mysqli_fetch_array($query_cat);
$get_cat_id = $cat_result['cat_id'];
// FACING THE ISSUE HERE
$cat_up = "UPDATE categories SET cat_name='$category'
cat_img='$filename' WHERE
cat_id='$get_cat_id'";
$query_up = mysqli_query($config, $cat_up);
if ($query_up) {
$msg = ["Post Publish Successfully", "alert-success"];
$_SESSION['msg'] = $msg;
// If the post publish successfully then redirect to
same page with a success message.
header("location:add_diary.php");
} else {
$msg = ["Failed, Please try again", "alert-danger"];
$_SESSION['msg'] = $msg;
// If the post is not able to post then redirect to the
same
page with an error message.
header("location:add_diary.php");
}
}
?>
I tried to insert using the id but didn't work and checked all the ways to insert and update that were available on the internet but didn't work
// This worked but it stays with the old image and doesn't update the new image.
$sql_up_cat = "UPDATE categories SET cat_img = (SELECT diary_thumbnail_image FROM diary WHERE cat_id = blog_id) ";
$query_up = mysqli_query($config, $sql_up_cat);