-3

Notice: Undefined index: categoryID in C:\xampp\htdocs\mit\postCategory.php on line 14

I'm having this error in my PHP code below where I'm only getting the categoryID via session and storing it in a index/variable which is $categoryID and then I called this variable on line 20 where I have my $query. Did I miss something? Thank you for your help!

<?php

session_start();
include "dbconnect.php";
if (!isset($_SESSION['admin'])) {
    header("Location:index.php");
}
if (isset($_GET['categoryID'])) {
    $_SESSION['postCategory']['categoryID'] = $_GET['categoryID'];
}

if (isset($_POST['update'])) {
    $categoryID = $_GET['categoryID'];
    $postTitle = $_POST['postTitle'];
    $postDesc = $_POST['postDesc'];
    $postImage = $_FILES['image']['name'];
    $target = "postImage/".basename($postImage);

    $query = "UPDATE background SET postTitle = '$postTitle', postDesc = '$postDesc', postImage = '$postImage' WHERE post.categoryID ='$categoryID'";

    $result = mysqli_query($con, $query);

    if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        $msg = "Image uploaded successfully";
    } else {
        $msg = "Failed to upload image";
    }
}

?>


<form method="post" action="postCategory.php" enctype="multipart/form-data">
    <input type="text" name="postTitle" placeholder="Your Post Title">
    <br>
    <textarea name="postDesc" id="myTextarea">Your Post Description</textarea>
    <br>
    <input type="file" name="image">
    <br>
    <button type="submit" class="btn btn-primary" name="update">Post</button>

</form>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • You can edit line 14 to $categoryID=$_GET['categoryID'] ?? $_SESSION['postCategory']['categoryID']; so it first tries to get that variable from $_GET and if that fails it tries to get it from $_SESSION. – Natha Mar 08 '19 at 14:23

1 Answers1

0

You need to include the $_GET variable in your form action:

<form method="post" action="postCategory.php?categoryID=1" enctype="multipart/form-data">

Or add it as a hidden text field and reference it as a $_POST variable.

Daniel G
  • 539
  • 4
  • 10